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

taosdata / TDengine / #3612

14 Feb 2025 06:43AM UTC coverage: 63.513% (+0.06%) from 63.456%
#3612

push

travis-ci

web-flow
Merge pull request #29757 from taosdata/feat/TS-5486

feat: support interp function with time range and time interval

141465 of 286269 branches covered (49.42%)

Branch coverage included in aggregate %.

220292 of 283307 relevant lines covered (77.76%)

18570887.65 hits per line

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

69.94
/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) {
43,550✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
43,552✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
43,552✔
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;
43,552✔
39
}
40

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
43,690✔
42
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
43,690✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
63,974✔
44
    (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
20,358✔
45
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
20,351✔
46
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
59✔
47
      return false;
59✔
48
    }
49
  }
50
  SStmtQNode* orig = pStmt->queue.head;
43,505✔
51
  SStmtQNode* node = pStmt->queue.head->next;
43,505✔
52
  pStmt->queue.head = pStmt->queue.head->next;
43,505✔
53
  *param = node;
43,505✔
54

55
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
43,505✔
56
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
43,717✔
57

58

59
  *param = node;
43,629✔
60

61
  return true;
43,629✔
62
}
63

64
void stmtEnqueue(STscStmt* pStmt, SStmtQNode* param) {
43,633✔
65
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
43,633✔
66

67
  pStmt->queue.tail->next = param;
43,662✔
68
  pStmt->queue.tail = param;
43,662✔
69

70
  pStmt->stat.bindDataNum++;
43,662✔
71
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
43,662✔
72
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
43,681✔
73

74
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
43,644✔
75
}
43,665✔
76

77
static int32_t stmtCreateRequest(STscStmt* pStmt) {
4,993,424✔
78
  int32_t code = 0;
4,993,424✔
79

80
  if (pStmt->exec.pRequest == NULL) {
4,993,424✔
81
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
113,646✔
82
                        pStmt->reqid);
83
    if (pStmt->reqid != 0) {
113,646!
84
      pStmt->reqid++;
×
85
    }
86
    if (TSDB_CODE_SUCCESS == code) {
113,646!
87
      pStmt->exec.pRequest->syncQuery = true;
113,646✔
88
      pStmt->exec.pRequest->isStmtBind = true;
113,646✔
89
    }
90
  }
91

92
  return code;
4,993,424✔
93
}
94

95
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
6,395,711✔
96
  int32_t code = 0;
6,395,711✔
97

98
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
6,395,711!
99
    STMT_LOG_SEQ(newStatus);
6,398,309✔
100
  }
101

102
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
6,440,169!
103
    STMT_DLOG("stmt already failed with err: %s", tstrerror(pStmt->errCode));
×
104
    return pStmt->errCode;
×
105
  }
106

107
  switch (newStatus) {
6,440,169!
108
    case STMT_PREPARE:
1,150✔
109
      pStmt->errCode = 0;
1,150✔
110
      break;
1,150✔
111
    case STMT_SETTBNAME:
128,386✔
112
      if (STMT_STATUS_EQ(INIT)) {
128,386!
113
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
114
      }
115
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
128,386!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      break;
128,386✔
119
    case STMT_SETTAGS:
40,134✔
120
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
40,134!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
40,134✔
124
    case STMT_FETCH_FIELDS:
2,614,545✔
125
      if (STMT_STATUS_EQ(INIT)) {
2,614,545!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
2,614,545✔
129
    case STMT_BIND:
2,285,412✔
130
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
2,285,412!
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      /*
134
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
135
              code = TSDB_CODE_TSC_STMT_API_ERROR;
136
            }
137
      */
138
      break;
2,285,412✔
139
    case STMT_BIND_COL:
×
140
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
141
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
142
      }
143
      break;
×
144
    case STMT_ADD_BATCH:
1,254,549✔
145
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
1,254,549!
146
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
147
      }
148
      break;
1,254,549✔
149
    case STMT_EXECUTE:
115,993✔
150
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
115,993✔
151
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
21,804!
152
            STMT_STATUS_NE(BIND_COL)) {
×
153
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
154
        }
155
      } else {
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
94,189!
157
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
        }
159
      }
160
      break;
115,993✔
161
    default:
×
162
      code = TSDB_CODE_APP_ERROR;
×
163
      break;
×
164
  }
165

166
  STMT_ERR_RET(code);
6,440,169!
167

168
  pStmt->sql.status = newStatus;
6,440,169✔
169

170
  return TSDB_CODE_SUCCESS;
6,440,169✔
171
}
172

173
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
3,923✔
174
  STscStmt* pStmt = (STscStmt*)stmt;
3,923✔
175

176
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
3,923✔
177

178
  if ('\0' == pStmt->bInfo.tbName[0]) {
3,923✔
179
    tscError("no table name set");
2!
180
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
2!
181
  }
182

183
  *tbName = pStmt->bInfo.tbName;
3,920✔
184

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

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

199
  pRes->userFields = taosMemoryMalloc(size);
200
  if (pRes->userFields == NULL) {
201
    taosMemoryFreeClear(pRes->fields);
202
    STMT_ERR_RET(terrno);
203
  }
204

205
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
206
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
207

208
  return TSDB_CODE_SUCCESS;
209
}
210

211
int32_t stmtRestoreQueryFields(STscStmt* pStmt) {
212
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
213
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
214

215
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
216
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
217

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

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

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

246
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
3,923✔
247
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
3,923✔
248
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
3,923✔
249

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

258
  return TSDB_CODE_SUCCESS;
3,923✔
259
}
260

261
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
3,920✔
262
  STscStmt* pStmt = (STscStmt*)stmt;
3,920✔
263

264
  pStmt->sql.pVgHash = pVgHash;
3,920✔
265
  pStmt->exec.pBlockHash = pBlockHash;
3,920✔
266

267
  return TSDB_CODE_SUCCESS;
3,920✔
268
}
269

270
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
3,925✔
271
                       SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
272
  STscStmt* pStmt = (STscStmt*)stmt;
3,925✔
273

274
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl));
3,925!
275
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
3,923!
276

277
  pStmt->sql.autoCreateTbl = autoCreateTbl;
3,922✔
278
  if (pStmt->sql.autoCreateTbl) {
3,922✔
279
    pStmt->sql.stbInterlaceMode = false;
204✔
280
  }
281

282
  return TSDB_CODE_SUCCESS;
3,922✔
283
}
284

285
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
2,997✔
286
  STscStmt* pStmt = (STscStmt*)stmt;
2,997✔
287

288
  *pVgHash = pStmt->sql.pVgHash;
2,997✔
289
  pStmt->sql.pVgHash = NULL;
2,997✔
290

291
  *pBlockHash = pStmt->exec.pBlockHash;
2,997✔
292
  pStmt->exec.pBlockHash = NULL;
2,997✔
293

294
  return TSDB_CODE_SUCCESS;
2,997✔
295
}
296

297
int32_t stmtCacheBlock(STscStmt* pStmt) {
1,249,231✔
298
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
1,249,231✔
299
    return TSDB_CODE_SUCCESS;
11,109✔
300
  }
301

302
  uint64_t uid = pStmt->bInfo.tbUid;
1,238,122✔
303
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
1,238,122✔
304

305
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
1,238,122✔
306
    return TSDB_CODE_SUCCESS;
1,236,048✔
307
  }
308

309
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,623✔
310
  if (!pSrc) {
3,843!
311
    return terrno;
×
312
  }
313
  STableDataCxt* pDst = NULL;
3,843✔
314

315
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3,843!
316

317
  SStmtTableCache cache = {
3,839✔
318
      .pDataCtx = pDst,
319
      .boundTags = pStmt->bInfo.boundTags,
3,839✔
320
  };
321

322
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
3,839!
323
    return terrno;
×
324
  }
325

326
  if (pStmt->sql.autoCreateTbl) {
3,843✔
327
    pStmt->bInfo.tagsCached = true;
202✔
328
  } else {
329
    pStmt->bInfo.boundTags = NULL;
3,641✔
330
  }
331

332
  return TSDB_CODE_SUCCESS;
3,843✔
333
}
334

335
int32_t stmtParseSql(STscStmt* pStmt) {
4,143✔
336
  pStmt->exec.pCurrBlock = NULL;
4,143✔
337

338
  SStmtCallback stmtCb = {
4,143✔
339
      .pStmt = pStmt,
340
      .getTbNameFn = stmtGetTbName,
341
      .setInfoFn = stmtUpdateInfo,
342
      .getExecInfoFn = stmtGetExecInfo,
343
  };
344

345
  STMT_ERR_RET(stmtCreateRequest(pStmt));
4,143!
346

347
  pStmt->stat.parseSqlNum++;
4,142✔
348
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
4,142✔
349
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
4,127✔
350

351
  pStmt->bInfo.needParse = false;
4,127✔
352

353
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
4,127✔
354
    pStmt->sql.type = STMT_TYPE_INSERT;
13✔
355
    pStmt->sql.stbInterlaceMode = false;
13✔
356
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
4,114✔
357
    pStmt->sql.type = STMT_TYPE_QUERY;
204✔
358
    pStmt->sql.stbInterlaceMode = false;
204✔
359

360
    return TSDB_CODE_SUCCESS;
204✔
361
  }
362

363
  STableDataCxt** pSrc =
364
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,923✔
365
  if (NULL == pSrc || NULL == *pSrc) {
3,926!
366
    return terrno;
×
367
  }
368

369
  STableDataCxt* pTableCtx = *pSrc;
3,926✔
370
  if (pStmt->sql.stbInterlaceMode) {
3,926✔
371
    int16_t lastIdx = -1;
58✔
372

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

379
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
440✔
380
    }
381
  }
382

383
  if (NULL == pStmt->sql.pBindInfo) {
3,926✔
384
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
929!
385
    if (NULL == pStmt->sql.pBindInfo) {
929!
386
      return terrno;
×
387
    }
388
  }
389

390
  return TSDB_CODE_SUCCESS;
3,926✔
391
}
392

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

401
  pStmt->bInfo.tbName[0] = 0;
7,905✔
402
  pStmt->bInfo.tbFName[0] = 0;
7,905✔
403
  if (!pStmt->bInfo.tagsCached) {
7,905✔
404
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
7,379✔
405
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
7,377!
406
  }
407
  pStmt->bInfo.stbFName[0] = 0;
7,903✔
408

409
  return TSDB_CODE_SUCCESS;
7,903✔
410
}
411

412
void stmtFreeTableBlkList(STableColsData* pTb) {
×
413
  (void)qResetStmtColumns(pTb->aCol, true);
×
414
  taosArrayDestroy(pTb->aCol);
×
415
}
×
416

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

426
  (void)taosThreadMutexLock(&pQueue->mutex);
2,613✔
427
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
2,613✔
428
  pQueue->qRemainNum = 0;
2,613✔
429
  pQueue->head->next = NULL;
2,613✔
430
  (void)taosThreadMutexUnlock(&pQueue->mutex);
2,613✔
431
}
432

433
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
117,138✔
434
  if (pStmt->sql.stbInterlaceMode) {
117,138✔
435
    if (deepClean) {
2,668✔
436
      taosHashCleanup(pStmt->exec.pBlockHash);
57✔
437
      pStmt->exec.pBlockHash = NULL;
57✔
438

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

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

460
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
151,331✔
461
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
91,576✔
462
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
204,956!
463

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

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

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

474
    if (keepTable) {
114,472✔
475
      return TSDB_CODE_SUCCESS;
113,380✔
476
    }
477

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

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

485
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,762!
486

487
  return TSDB_CODE_SUCCESS;
3,760✔
488
}
489

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

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

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

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

512
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
1,148✔
513
  while (pIter) {
4,990✔
514
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
3,844✔
515

516
    qDestroyStmtDataBlock(pCache->pDataCtx);
3,844✔
517
    qDestroyBoundColInfo(pCache->boundTags);
3,843✔
518
    taosMemoryFreeClear(pCache->boundTags);
3,842!
519

520
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
3,844✔
521
  }
522
  taosHashCleanup(pStmt->sql.pTableCache);
1,146✔
523
  pStmt->sql.pTableCache = NULL;
1,148✔
524

525
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
1,148!
526
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
1,148!
527

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

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

539
  STMT_DLOG_E("end to free SQL info");
1,147✔
540

541
  return TSDB_CODE_SUCCESS;
1,146✔
542
}
543

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

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

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

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

566
  *vgId = vgInfo.vgId;
39,679✔
567

568
  return TSDB_CODE_SUCCESS;
39,679✔
569
}
570

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

576
  STMT_DLOG("tableDataCxt rebuilt, uid:%" PRId64 ", vgId:%d", uid, vgId);
55,886!
577

578
  return TSDB_CODE_SUCCESS;
55,886✔
579
}
580

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

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

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

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

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

604
  if (NULL == pStmt->pCatalog) {
92,500✔
605
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
640!
606
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
648✔
607
  }
608

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

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

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

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

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

634
      pStmt->exec.pCurrBlock = pNewBlock;
39,678✔
635

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

638
      return TSDB_CODE_SUCCESS;
39,678✔
639
    }
640

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

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

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

655
  pStmt->stat.ctgGetTbMetaNum++;
51,875✔
656

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

661
    STMT_ERR_RET(code);
×
662
  }
663

664
  STMT_ERR_RET(code);
51,875!
665

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

672
  taosMemoryFree(pTableMeta);
51,875!
673

674
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
51,875✔
675

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

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

681
    return TSDB_CODE_SUCCESS;
16,470✔
682
  }
683

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

690
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
691
    }
692

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

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

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

703
    return TSDB_CODE_SUCCESS;
16,200✔
704
  }
705

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

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

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

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

724
    pStmt->exec.pCurrBlock = pNewBlock;
16,208✔
725

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

728
    return TSDB_CODE_SUCCESS;
16,208✔
729
  }
730

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

733
  return TSDB_CODE_SUCCESS;
2,997✔
734
}
735

736
int32_t stmtResetStmt(STscStmt* pStmt) {
458✔
737
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
458!
738

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

744
  pStmt->sql.status = STMT_INIT;
458✔
745

746
  return TSDB_CODE_SUCCESS;
458✔
747
}
748

749
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
43,606✔
750
  SStmtQNode* pParam = (SStmtQNode*)param;
43,606✔
751

752
  if (pParam->restoreTbCols) {
43,606✔
753
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
43,603✔
754
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
40,995✔
755
      *p = taosArrayInit(20, POINTER_BYTES);
40,995✔
756
      if (*p == NULL) {
40,998!
757
        STMT_ERR_RET(terrno);
×
758
      }
759
    }
760

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

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

768
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
40,817✔
769
  }
770
  return TSDB_CODE_SUCCESS;
43,687✔
771
}
772

773
void* stmtBindThreadFunc(void* param) {
59✔
774
  setThreadName("stmtBind");
59✔
775

776
  qInfo("stmt bind thread started");
59!
777

778
  STscStmt* pStmt = (STscStmt*)param;
59✔
779

780
  while (true) {
43,734✔
781
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
43,793✔
782
      break;
59✔
783
    }
784

785
    SStmtQNode* asyncParam = NULL;
43,703✔
786
    if (!stmtDequeue(pStmt, &asyncParam)) {
43,703✔
787
      continue;
59✔
788
    }
789

790
    int ret = stmtAsyncOutput(pStmt, asyncParam);
43,633✔
791
    if (ret != 0) {
43,678!
792
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
793
    }
794
  }
795

796
  qInfo("stmt bind thread stopped");
59!
797

798
  return NULL;
59✔
799
}
800

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

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

815
  pStmt->bindThreadInUse = true;
59✔
816

817
  (void)taosThreadAttrDestroy(&thAttr);
59✔
818
  return TSDB_CODE_SUCCESS;
59✔
819
}
820

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

827
  return TSDB_CODE_SUCCESS;
59✔
828
}
829

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

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

846
  pTblBuf->pCurBuff = buff;
59✔
847
  pTblBuf->buffIdx = 1;
59✔
848
  pTblBuf->buffOffset = 0;
59✔
849

850
  return TSDB_CODE_SUCCESS;
59✔
851
}
852

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

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

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

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

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

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

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

912
  pStmt->sql.siInfo.tableColsReady = true;
691✔
913

914
  STMT_LOG_SEQ(STMT_INIT);
691✔
915

916
  tscDebug("stmt:%p initialized", pStmt);
691✔
917

918
  return pStmt;
690✔
919
}
920

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

924
  STMT_DLOG_E("start to prepare");
1,150✔
925

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

930
  if (pStmt->sql.status >= STMT_PREPARE) {
1,150✔
931
    STMT_ERR_RET(stmtResetStmt(pStmt));
458!
932
  }
933

934
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
1,150!
935

936
  if (length <= 0) {
1,149✔
937
    length = strlen(sql);
1,038✔
938
  }
939

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

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

953
  return TSDB_CODE_SUCCESS;
1,150✔
954
}
955

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

963
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
55!
964
  pStmt->sql.siInfo.pDataCtx = pDst;
50✔
965

966
  SArray* pTblCols = NULL;
50✔
967
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
46,779✔
968
    pTblCols = taosArrayInit(20, POINTER_BYTES);
46,762✔
969
    if (NULL == pTblCols) {
48,539!
970
      return terrno;
×
971
    }
972

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

978
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
17✔
979

980
  return TSDB_CODE_SUCCESS;
17✔
981
}
982

983
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
3✔
984
  STscStmt* pStmt = (STscStmt*)stmt;
3✔
985

986
  STMT_DLOG("start to set dbName: %s", dbName);
3!
987

988
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3!
989

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

999
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
128,396✔
1000
  STscStmt* pStmt = (STscStmt*)stmt;
128,396✔
1001

1002
  int64_t startUs = taosGetTimestampUs();
128,404✔
1003

1004
  STMT_DLOG("start to set tbName: %s", tbName);
128,404✔
1005

1006
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
128,405!
1007
    return pStmt->errCode;
×
1008
  }
1009

1010
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
128,405!
1011

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

1019
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
128,330✔
1020
    STMT_ERR_RET(stmtCreateRequest(pStmt));
92,792!
1021

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

1026
    STMT_ERR_RET(stmtGetFromCache(pStmt));
92,791!
1027

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

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

1041
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
128,321✔
1042
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
57!
1043
  }
1044

1045
  int64_t startUs2 = taosGetTimestampUs();
128,312✔
1046
  pStmt->stat.setTbNameUs += startUs2 - startUs;
128,312✔
1047

1048
  return TSDB_CODE_SUCCESS;
128,312✔
1049
}
1050

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

1054
  STMT_DLOG_E("start to set tbTags");
40,134✔
1055

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

1060
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
40,134!
1061

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

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

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

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

1084
  return TSDB_CODE_SUCCESS;
39,864✔
1085
}
1086

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

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

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

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

1106
  return TSDB_CODE_SUCCESS;
20,103✔
1107
}
1108

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

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

1119
  STableDataCxt** pDataBlock = NULL;
2,594,238✔
1120

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

1132
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
2,594,238!
1133

1134
  return TSDB_CODE_SUCCESS;
2,594,238✔
1135
}
1136

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

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

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

1155
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
40,945✔
1156
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
40,945✔
1157
    pStmt->sql.siInfo.pVgroupHash =
2,611✔
1158
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,608✔
1159
  }
1160
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
40,948✔
1161
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
2,612✔
1162
  }
1163

1164
  if (NULL == pStmt->sql.siInfo.pRequest) {
40,948✔
1165
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
57!
1166
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1167

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

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

1177
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
40,948✔
1178
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
284✔
1179
    pStmt->sql.siInfo.tbFromHash = true;
35✔
1180
  }
1181

1182
  if (0 == pStmt->sql.siInfo.firstName[0]) {
40,948✔
1183
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
57✔
1184
  }
1185

1186
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
40,948✔
1187
  param->next = NULL;
40,948✔
1188

1189
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
40,948✔
1190

1191
  stmtEnqueue(pStmt, param);
41,045✔
1192

1193
  return TSDB_CODE_SUCCESS;
41,043✔
1194
}
1195

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

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

1216
  return TSDB_CODE_SUCCESS;
40,880✔
1217
}
1218

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

1223
  int64_t startUs = taosGetTimestampUs();
2,291,098✔
1224

1225
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
2,291,098✔
1226

1227
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,288,970✔
1228
    return pStmt->errCode;
10✔
1229
  }
1230

1231
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
2,288,960!
1232

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

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

1243
  STMT_ERR_RET(stmtCreateRequest(pStmt));
2,282,370!
1244

1245
  if (pStmt->bInfo.needParse) {
2,278,906✔
1246
    STMT_ERR_RET(stmtParseSql(pStmt));
191!
1247
  }
1248

1249
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,280,104✔
1250
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
21,804!
1251

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

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

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

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

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

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

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

1287
    return TSDB_CODE_SUCCESS;
21,804✔
1288
  }
1289

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

1294
  STableDataCxt** pDataBlock = NULL;
2,257,094✔
1295

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

1312
  int64_t startUs2 = taosGetTimestampUs();
2,255,395✔
1313
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
2,255,395✔
1314

1315
  SStmtQNode* param = NULL;
2,255,395✔
1316
  if (pStmt->sql.stbInterlaceMode) {
2,255,395✔
1317
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
81,758!
1318
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
81,760!
1319
    taosArrayClear(param->tblData.aCol);
40,880✔
1320

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

1323
    param->restoreTbCols = false;
40,870✔
1324
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
40,870✔
1325
  }
1326

1327
  int64_t startUs3 = taosGetTimestampUs();
2,253,907✔
1328
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
2,253,907✔
1329

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

1332
  if (colIdx < 0) {
2,253,907✔
1333
    if (pStmt->sql.stbInterlaceMode) {
1,154,017✔
1334
      (*pDataBlock)->pData->flags = 0;
40,895✔
1335
      code =
1336
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
40,895✔
1337
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
40,895✔
1338
    } else {
1339
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
1,113,122✔
1340
                                pStmt->taos->optionInfo.charsetCxt);
1,113,122✔
1341
    }
1342

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

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

1358
    pStmt->bInfo.sBindLastIdx = colIdx;
1,099,890✔
1359

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

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

1373
  int64_t startUs4 = taosGetTimestampUs();
2,243,591✔
1374
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
2,243,591✔
1375

1376
  if (pStmt->sql.stbInterlaceMode) {
2,243,591✔
1377
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
40,963!
1378
  }
1379

1380
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
2,250,996✔
1381

1382
  return TSDB_CODE_SUCCESS;
2,250,996✔
1383
}
1384

1385
int stmtAddBatch(TAOS_STMT* stmt) {
1,255,412✔
1386
  STscStmt* pStmt = (STscStmt*)stmt;
1,255,412✔
1387

1388
  int64_t startUs = taosGetTimestampUs();
1,251,467✔
1389

1390
  STMT_DLOG_E("start to add batch");
1,251,467✔
1391

1392
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,252,352✔
1393
    return pStmt->errCode;
10✔
1394
  }
1395

1396
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
1,252,342!
1397

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

1402
    pStmt->sql.siInfo.tableColsReady = false;
2,613✔
1403

1404
    SStmtQNode* param = NULL;
2,613✔
1405
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
5,226!
1406
    param->restoreTbCols = true;
2,613✔
1407
    param->next = NULL;
2,613✔
1408

1409
    stmtEnqueue(pStmt, param);
2,613✔
1410

1411
    return TSDB_CODE_SUCCESS;
2,614✔
1412
  }
1413

1414
  STMT_ERR_RET(stmtCacheBlock(pStmt));
1,250,863✔
1415

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

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

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

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

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

1448
      break;
1449
    }
1450

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

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

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

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

1482
      pStmt->stat.ctgGetTbMetaNum++;
1483

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

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

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

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

1502
  return finalCode;
1503
}
1504
*/
1505

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

1515
  STMT_DLOG_E("start to exec");
1516

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

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

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

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

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

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

1540
_return:
1541

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

1544
  tFreeSSubmitRsp(pRsp);
1545

1546
  ++pStmt->sql.runTimes;
1547

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

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

1557
  int64_t startUs = taosGetTimestampUs();
116,003✔
1558

1559
  STMT_DLOG_E("start to exec");
116,003✔
1560

1561
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
116,003✔
1562
    return pStmt->errCode;
10✔
1563
  }
1564

1565
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
115,993!
1566

1567
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
115,993✔
1568
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
21,804✔
1569
  } else {
1570
    if (pStmt->sql.stbInterlaceMode) {
94,189✔
1571
      int64_t startTs = taosGetTimestampUs();
2,612✔
1572
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
5,059✔
1573
        taosUsleep(1);
2,443✔
1574
      }
1575
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
2,614✔
1576

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

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

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

1590
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
94,191✔
1591
  }
1592

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

1604
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
115,992✔
1605

1606
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
115,991✔
1607
  pStmt->affectedRows += pStmt->exec.affectedRows;
115,992✔
1608

1609
_return:
115,993✔
1610

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

1615
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
115,993!
1616

1617
  tFreeSSubmitRsp(pRsp);
115,992✔
1618

1619
  ++pStmt->sql.runTimes;
115,993✔
1620

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

1624
  STMT_RET(code);
115,992✔
1625
}
1626

1627
int stmtClose(TAOS_STMT* stmt) {
690✔
1628
  STscStmt* pStmt = (STscStmt*)stmt;
690✔
1629

1630
  STMT_DLOG_E("start to free stmt");
690✔
1631

1632
  pStmt->queue.stopQueue = true;
690✔
1633

1634
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
690✔
1635
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
690✔
1636
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
690✔
1637

1638
  if (pStmt->bindThreadInUse) {
690✔
1639
    (void)taosThreadJoin(pStmt->bindThread, NULL);
59✔
1640
    pStmt->bindThreadInUse = false;
59✔
1641
  }
1642

1643
  (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
690✔
1644
  (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
689✔
1645

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

1657
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
688!
1658
  taosMemoryFree(stmt);
688!
1659

1660
  return TSDB_CODE_SUCCESS;
690✔
1661
}
1662

1663
const char* stmtErrstr(TAOS_STMT* stmt) {
4✔
1664
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1665

1666
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
4!
1667
    return (char*)tstrerror(terrno);
1✔
1668
  }
1669

1670
  pStmt->exec.pRequest->code = terrno;
3✔
1671

1672
  return taos_errstr(pStmt->exec.pRequest);
3✔
1673
}
1674

1675
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
297✔
1676

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

1679
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
2,435,461✔
1680
  STscStmt* pStmt = (STscStmt*)stmt;
2,435,461✔
1681

1682
  STMT_DLOG_E("start is insert");
2,435,461✔
1683

1684
  if (pStmt->sql.type) {
2,436,795✔
1685
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
2,435,036✔
1686
  } else {
1687
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
1,759✔
1688
  }
1689

1690
  return TSDB_CODE_SUCCESS;
2,436,795✔
1691
}
1692

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

1698
  STMT_DLOG_E("start to get tag fields");
20,106!
1699

1700
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,106!
1701
    return pStmt->errCode;
×
1702
  }
1703

1704
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
20,106!
1705
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1706
  }
1707

1708
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
20,106!
1709

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

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

1720
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
20,106!
1721

1722
  if (pStmt->bInfo.needParse) {
20,106✔
1723
    STMT_ERRI_JRET(stmtParseSql(pStmt));
3✔
1724
  }
1725

1726
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
20,104✔
1727

1728
_return:
20,103✔
1729

1730
  pStmt->errCode = preCode;
20,106✔
1731

1732
  return code;
20,106✔
1733
}
1734

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

1740
  STMT_DLOG_E("start to get col fields");
268,568!
1741

1742
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
268,568!
1743
    return pStmt->errCode;
×
1744
  }
1745

1746
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
268,568!
1747
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1748
  }
1749

1750
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
268,568!
1751

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

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

1763
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
268,568!
1764

1765
  if (pStmt->bInfo.needParse) {
268,568✔
1766
    STMT_ERRI_JRET(stmtParseSql(pStmt));
8!
1767
  }
1768

1769
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
268,568!
1770

1771
_return:
268,568✔
1772

1773
  pStmt->errCode = preCode;
268,568✔
1774

1775
  return code;
268,568✔
1776
}
1777

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

1783
  STMT_DLOG_E("start to get param num");
1,333!
1784

1785
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,333!
1786
    return pStmt->errCode;
×
1787
  }
1788

1789
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
1,333!
1790

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

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

1801
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
1,333!
1802

1803
  if (pStmt->bInfo.needParse) {
1,333✔
1804
    STMT_ERRI_JRET(stmtParseSql(pStmt));
22✔
1805
  }
1806

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

1813
_return:
1,132✔
1814

1815
  pStmt->errCode = preCode;
1,333✔
1816

1817
  return code;
1,333✔
1818
}
1819

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

1825
  STMT_DLOG_E("start to get param");
2,324,538!
1826

1827
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,324,538!
1828
    return pStmt->errCode;
×
1829
  }
1830

1831
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,324,538!
1832
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1833
  }
1834

1835
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2,324,538!
1836

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

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

1847
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2,324,538!
1848

1849
  if (pStmt->bInfo.needParse) {
2,324,538!
1850
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1851
  }
1852

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

1861
  *type = pField[idx].type;
2,324,538✔
1862
  *bytes = pField[idx].bytes;
2,324,538✔
1863

1864
_return:
2,324,538✔
1865

1866
  taosMemoryFree(pField);
2,324,538!
1867
  pStmt->errCode = preCode;
2,324,538✔
1868

1869
  return code;
2,324,538✔
1870
}
1871

1872
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
21,804✔
1873
  STscStmt* pStmt = (STscStmt*)stmt;
21,804✔
1874

1875
  STMT_DLOG_E("start to use result");
21,804✔
1876

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

1882
  return pStmt->exec.pRequest;
21,804✔
1883
}
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