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

taosdata / TDengine / #3594

24 Jan 2025 08:57AM UTC coverage: 63.086% (-0.2%) from 63.239%
#3594

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

140232 of 285630 branches covered (49.1%)

Branch coverage included in aggregate %.

218398 of 282844 relevant lines covered (77.22%)

18911829.37 hits per line

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

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

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
5,624✔
42
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
5,624✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
11,013✔
44
    (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
5,405✔
45
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
5,405✔
46
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
16✔
47
      return false;
16✔
48
    }
49
  }
50
  SStmtQNode* orig = pStmt->queue.head;
5,608✔
51
  SStmtQNode* node = pStmt->queue.head->next;
5,608✔
52
  pStmt->queue.head = pStmt->queue.head->next;
5,608✔
53
  *param = node;
5,608✔
54

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

58

59
  *param = node;
5,608✔
60

61
  return true;
5,608✔
62
}
63

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

67
  pStmt->queue.tail->next = param;
5,608✔
68
  pStmt->queue.tail = param;
5,608✔
69

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

74
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
5,608✔
75
}
5,608✔
76

77
static int32_t stmtCreateRequest(STscStmt* pStmt) {
3,974,698✔
78
  int32_t code = 0;
3,974,698✔
79

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

92
  return code;
3,974,698✔
93
}
94

95
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
4,399,114✔
96
  int32_t code = 0;
4,399,114✔
97

98
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
4,399,114!
99
    STMT_LOG_SEQ(newStatus);
4,399,114✔
100
  }
101

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

107
  switch (newStatus) {
4,399,114!
108
    case STMT_PREPARE:
787✔
109
      pStmt->errCode = 0;
787✔
110
      break;
787✔
111
    case STMT_SETTBNAME:
92,540✔
112
      if (STMT_STATUS_EQ(INIT)) {
92,540!
113
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
114
      }
115
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
92,540!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      break;
92,540✔
119
    case STMT_SETTAGS:
40,133✔
120
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
40,133!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
40,133✔
124
    case STMT_FETCH_FIELDS:
2,614,306✔
125
      if (STMT_STATUS_EQ(INIT)) {
2,614,306!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
2,614,306✔
129
    case STMT_BIND:
1,264,167✔
130
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
1,264,167!
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;
1,264,167✔
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:
274,062✔
145
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
274,062!
146
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
147
      }
148
      break;
274,062✔
149
    case STMT_EXECUTE:
113,119✔
150
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
113,119✔
151
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
21,801!
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)) {
91,318!
157
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
        }
159
      }
160
      break;
113,119✔
161
    default:
×
162
      code = TSDB_CODE_APP_ERROR;
×
163
      break;
×
164
  }
165

166
  STMT_ERR_RET(code);
4,399,114!
167

168
  pStmt->sql.status = newStatus;
4,399,114✔
169

170
  return TSDB_CODE_SUCCESS;
4,399,114✔
171
}
172

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

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

178
  if ('\0' == pStmt->bInfo.tbName[0]) {
3,569✔
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,567✔
184

185
  return TSDB_CODE_SUCCESS;
3,567✔
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,569✔
238
                           bool autoCreateTbl) {
239
  STscStmt* pStmt = (STscStmt*)stmt;
3,569✔
240
  char      tbFName[TSDB_TABLE_FNAME_LEN];
241
  int32_t   code = tNameExtractFullName(tbName, tbFName);
3,569✔
242
  if (code != 0) {
3,569!
243
    return code;
×
244
  }
245

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

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

258
  return TSDB_CODE_SUCCESS;
3,569✔
259
}
260

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

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

267
  return TSDB_CODE_SUCCESS;
3,569✔
268
}
269

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

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

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

282
  return TSDB_CODE_SUCCESS;
3,569✔
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) {
273,947✔
298
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
273,947✔
299
    return TSDB_CODE_SUCCESS;
10,973✔
300
  }
301

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

305
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
262,974✔
306
    return TSDB_CODE_SUCCESS;
259,442✔
307
  }
308

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

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

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

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

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

332
  return TSDB_CODE_SUCCESS;
3,532✔
333
}
334

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

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

345
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3,784!
346

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

351
  pStmt->bInfo.needParse = false;
3,770✔
352

353
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
3,770✔
354
    pStmt->sql.type = STMT_TYPE_INSERT;
11✔
355
    pStmt->sql.stbInterlaceMode = false;
11✔
356
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
3,759✔
357
    pStmt->sql.type = STMT_TYPE_QUERY;
201✔
358
    pStmt->sql.stbInterlaceMode = false;
201✔
359

360
    return TSDB_CODE_SUCCESS;
201✔
361
  }
362

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

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

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

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

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

390
  return TSDB_CODE_SUCCESS;
3,569✔
391
}
392

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

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

409
  return TSDB_CODE_SUCCESS;
4,684✔
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) {
115✔
418
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
115✔
419
  if (NULL == pTblBuf->pCurBuff) {
115!
420
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
421
    return;
×
422
  }
423
  pTblBuf->buffIdx = 1;
115✔
424
  pTblBuf->buffOffset = sizeof(*pQueue->head);
115✔
425

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

433
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
113,905✔
434
  if (pStmt->sql.stbInterlaceMode) {
113,905✔
435
    if (deepClean) {
129✔
436
      taosHashCleanup(pStmt->exec.pBlockHash);
14✔
437
      pStmt->exec.pBlockHash = NULL;
14✔
438

439
      if (NULL != pStmt->exec.pCurrBlock) {
14!
440
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
14!
441
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
14✔
442
      }
443
    } else {
444
      pStmt->sql.siInfo.pTableColsIdx = 0;
115✔
445
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
115✔
446
    }
447
  } else {
448
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
113,776✔
449
      taos_free_result(pStmt->exec.pRequest);
91,975✔
450
      pStmt->exec.pRequest = NULL;
91,975✔
451
    }
452

453
    size_t keyLen = 0;
113,776✔
454
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
113,776✔
455
    while (pIter) {
264,420✔
456
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
150,644✔
457
      char*          key = taosHashGetKey(pIter, &keyLen);
150,644✔
458
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
150,644✔
459

460
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
150,644✔
461
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
91,203✔
462
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
204,207!
463

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

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

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

474
    if (keepTable) {
113,776✔
475
      return TSDB_CODE_SUCCESS;
113,004✔
476
    }
477

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

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

485
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
901!
486

487
  return TSDB_CODE_SUCCESS;
901✔
488
}
489

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

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

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

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

512
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
786✔
513
  while (pIter) {
4,318✔
514
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
3,532✔
515

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

520
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
3,532✔
521
  }
522
  taosHashCleanup(pStmt->sql.pTableCache);
786✔
523
  pStmt->sql.pTableCache = NULL;
786✔
524

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

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

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

539
  STMT_DLOG_E("end to free SQL info");
786✔
540

541
  return TSDB_CODE_SUCCESS;
786✔
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,439✔
582
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
92,439!
583
    pStmt->bInfo.needParse = false;
×
584
    pStmt->bInfo.inExecCache = false;
×
585
    return TSDB_CODE_SUCCESS;
×
586
  }
587

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

591
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
92,439✔
592
  if (pCxtInExec) {
92,438✔
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,152✔
605
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
323!
606
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
324✔
607
  }
608

609
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
92,153!
610
    if (pStmt->bInfo.inExecCache) {
600✔
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);
570!
617
    return TSDB_CODE_SUCCESS;
570✔
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) {
427✔
737
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
427!
738

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

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

746
  return TSDB_CODE_SUCCESS;
427✔
747
}
748

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

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

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

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

768
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,493✔
769
  }
770
  return TSDB_CODE_SUCCESS;
5,608✔
771
}
772

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

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

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

780
  while (true) {
5,624✔
781
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
5,640✔
782
      break;
16✔
783
    }
784

785
    SStmtQNode* asyncParam = NULL;
5,624✔
786
    if (!stmtDequeue(pStmt, &asyncParam)) {
5,624✔
787
      continue;
16✔
788
    }
789

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

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

798
  return NULL;
16✔
799
}
800

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

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

815
  pStmt->bindThreadInUse = true;
16✔
816

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

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

827
  return TSDB_CODE_SUCCESS;
16✔
828
}
829

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

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

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

850
  return TSDB_CODE_SUCCESS;
16✔
851
}
852

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

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

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

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

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

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

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

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

914
  STMT_LOG_SEQ(STMT_INIT);
360✔
915

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

918
  return pStmt;
360✔
919
}
920

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

924
  STMT_DLOG_E("start to prepare");
787✔
925

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

930
  if (pStmt->sql.status >= STMT_PREPARE) {
787✔
931
    STMT_ERR_RET(stmtResetStmt(pStmt));
427!
932
  }
933

934
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
787!
935

936
  if (length <= 0) {
787✔
937
    length = strlen(sql);
765✔
938
  }
939

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

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

953
  return TSDB_CODE_SUCCESS;
787✔
954
}
955

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

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

966
  SArray* pTblCols = NULL;
14✔
967
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
13,539✔
968
    pTblCols = taosArrayInit(20, POINTER_BYTES);
13,520✔
969
    if (NULL == pTblCols) {
13,621!
970
      return terrno;
×
971
    }
972

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

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

980
  return TSDB_CODE_SUCCESS;
19✔
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) {
92,540✔
1000
  STscStmt* pStmt = (STscStmt*)stmt;
92,540✔
1001

1002
  int64_t startUs = taosGetTimestampUs();
92,540✔
1003

1004
  STMT_DLOG("start to set tbName: %s", tbName);
92,540!
1005

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

1010
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
92,540!
1011

1012
  int32_t insert = 0;
92,540✔
1013
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
92,540!
1014
  if (0 == insert) {
92,540!
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) {
92,540✔
1020
    STMT_ERR_RET(stmtCreateRequest(pStmt));
92,439!
1021

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

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

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

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

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

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

1048
  return TSDB_CODE_SUCCESS;
92,530✔
1049
}
1050

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

1054
  STMT_DLOG_E("start to set tbTags");
40,133!
1055

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

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

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

1068
  if (pStmt->bInfo.inExecCache) {
40,133✔
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,863✔
1074
  if (NULL == pDataBlock) {
39,863!
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,863!
1080
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
39,863!
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,863✔
1085
}
1086

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

1092
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
19,864!
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));
19,864✔
1099
  if (NULL == pDataBlock) {
19,864!
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));
19,864!
1105

1106
  return TSDB_CODE_SUCCESS;
19,864✔
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) {
5,492✔
1156
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,492✔
1157
    pStmt->sql.siInfo.pVgroupHash =
115✔
1158
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
114✔
1159
  }
1160
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,493✔
1161
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
115✔
1162
  }
1163

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

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

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

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

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

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

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

1191
  stmtEnqueue(pStmt, param);
5,493✔
1192

1193
  return TSDB_CODE_SUCCESS;
5,493✔
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)) {
5,493!
1199
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,493✔
1200
      break;
5,493✔
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;
5,493✔
1217
}
1218

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

1223
  int64_t startUs = taosGetTimestampUs();
1,264,177✔
1224

1225
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
1,264,177✔
1226

1227
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,264,177✔
1228
    return pStmt->errCode;
10✔
1229
  }
1230

1231
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
1,264,167!
1232

1233
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,264,167!
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) {
1,264,167✔
1239
    taos_free_result(pStmt->exec.pRequest);
21,600✔
1240
    pStmt->exec.pRequest = NULL;
21,600✔
1241
  }
1242

1243
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,264,167!
1244

1245
  if (pStmt->bInfo.needParse) {
1,264,167✔
1246
    STMT_ERR_RET(stmtParseSql(pStmt));
184✔
1247
  }
1248

1249
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,264,166✔
1250
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
21,801!
1251

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

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

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

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

1277
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
21,801✔
1278
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
21,801✔
1279
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
21,801✔
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,801✔
1288
  }
1289

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

1294
  STableDataCxt** pDataBlock = NULL;
1,242,365✔
1295

1296
  if (pStmt->exec.pCurrBlock) {
1,242,365✔
1297
    pDataBlock = &pStmt->exec.pCurrBlock;
1,239,101✔
1298
  } else {
1299
    pDataBlock =
1300
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,264✔
1301
    if (NULL == pDataBlock) {
3,264!
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,264✔
1306
    if (pStmt->sql.stbInterlaceMode) {
3,264✔
1307
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
14✔
1308
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
14✔
1309
    }
1310
  }
1311

1312
  int64_t startUs2 = taosGetTimestampUs();
1,242,365✔
1313
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
1,242,365✔
1314

1315
  SStmtQNode* param = NULL;
1,242,365✔
1316
  if (pStmt->sql.stbInterlaceMode) {
1,242,365✔
1317
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
10,986!
1318
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
10,986!
1319
    taosArrayClear(param->tblData.aCol);
5,493✔
1320

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

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

1327
  int64_t startUs3 = taosGetTimestampUs();
1,242,365✔
1328
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
1,242,365✔
1329

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

1332
  if (colIdx < 0) {
1,242,365✔
1333
    if (pStmt->sql.stbInterlaceMode) {
140,063✔
1334
      (*pDataBlock)->pData->flags = 0;
5,493✔
1335
      code =
1336
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
5,493✔
1337
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
5,493✔
1338
    } else {
1339
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
134,570✔
1340
                                pStmt->taos->optionInfo.charsetCxt);
134,570✔
1341
    }
1342

1343
    if (code) {
140,063✔
1344
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
10!
1345
      STMT_ERR_RET(code);
10!
1346
    }
1347
  } else {
1348
    if (pStmt->sql.stbInterlaceMode) {
1,102,302!
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,102,302!
1354
      tscError("bind column index not in sequence");
×
1355
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1356
    }
1357

1358
    pStmt->bInfo.sBindLastIdx = colIdx;
1,102,302✔
1359

1360
    if (0 == colIdx) {
1,102,302✔
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,102,302✔
1366
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
1,102,302✔
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();
1,242,355✔
1374
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
1,242,355✔
1375

1376
  if (pStmt->sql.stbInterlaceMode) {
1,242,355✔
1377
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,493!
1378
  }
1379

1380
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
1,242,355✔
1381

1382
  return TSDB_CODE_SUCCESS;
1,242,355✔
1383
}
1384

1385
int stmtAddBatch(TAOS_STMT* stmt) {
274,072✔
1386
  STscStmt* pStmt = (STscStmt*)stmt;
274,072✔
1387

1388
  int64_t startUs = taosGetTimestampUs();
274,072✔
1389

1390
  STMT_DLOG_E("start to add batch");
274,072✔
1391

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

1396
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
274,062!
1397

1398
  if (pStmt->sql.stbInterlaceMode) {
274,062✔
1399
    int64_t startUs2 = taosGetTimestampUs();
115✔
1400
    pStmt->stat.addBatchUs += startUs2 - startUs;
115✔
1401

1402
    pStmt->sql.siInfo.tableColsReady = false;
115✔
1403

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

1409
    stmtEnqueue(pStmt, param);
115✔
1410

1411
    return TSDB_CODE_SUCCESS;
115✔
1412
  }
1413

1414
  STMT_ERR_RET(stmtCacheBlock(pStmt));
273,947!
1415

1416
  return TSDB_CODE_SUCCESS;
273,947✔
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) {
113,129✔
1553
  STscStmt*   pStmt = (STscStmt*)stmt;
113,129✔
1554
  int32_t     code = 0;
113,129✔
1555
  SSubmitRsp* pRsp = NULL;
113,129✔
1556

1557
  int64_t startUs = taosGetTimestampUs();
113,129✔
1558

1559
  STMT_DLOG_E("start to exec");
113,129✔
1560

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

1565
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
113,119!
1566

1567
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
113,119✔
1568
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
21,801✔
1569
  } else {
1570
    if (pStmt->sql.stbInterlaceMode) {
91,318✔
1571
      int64_t startTs = taosGetTimestampUs();
115✔
1572
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
240✔
1573
        taosUsleep(1);
125✔
1574
      }
1575
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
115✔
1576

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

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

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

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

1593
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
113,119!
1594
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
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);
113,119!
1605

1606
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
113,119✔
1607
  pStmt->affectedRows += pStmt->exec.affectedRows;
113,119✔
1608

1609
_return:
113,119✔
1610

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

1615
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
113,119!
1616

1617
  tFreeSSubmitRsp(pRsp);
113,119✔
1618

1619
  ++pStmt->sql.runTimes;
113,119✔
1620

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

1624
  STMT_RET(code);
113,119!
1625
}
1626

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

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

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

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

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

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

1646
  STMT_DLOG("stmt %p closed, stbInterlaceMode: %d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
359✔
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));
359!
1658
  taosMemoryFree(stmt);
359!
1659

1660
  return TSDB_CODE_SUCCESS;
359✔
1661
}
1662

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

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

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

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

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

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

1679
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
1,365,902✔
1680
  STscStmt* pStmt = (STscStmt*)stmt;
1,365,902✔
1681

1682
  STMT_DLOG_E("start is insert");
1,365,902✔
1683

1684
  if (pStmt->sql.type) {
1,365,902✔
1685
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,364,498✔
1686
  } else {
1687
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
1,404✔
1688
  }
1689

1690
  return TSDB_CODE_SUCCESS;
1,365,902✔
1691
}
1692

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

1698
  STMT_DLOG_E("start to get tag fields");
19,867!
1699

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

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

1708
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
19,867!
1709

1710
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
19,867!
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) {
19,867!
1716
    taos_free_result(pStmt->exec.pRequest);
×
1717
    pStmt->exec.pRequest = NULL;
×
1718
  }
1719

1720
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
19,867!
1721

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

1726
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
19,865✔
1727

1728
_return:
19,864✔
1729

1730
  pStmt->errCode = preCode;
19,867✔
1731

1732
  return code;
19,867✔
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,801✔
1873
  STscStmt* pStmt = (STscStmt*)stmt;
21,801✔
1874

1875
  STMT_DLOG_E("start to use result");
21,801!
1876

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

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