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

taosdata / TDengine / #4623

30 Jul 2025 02:24AM UTC coverage: 60.377% (-0.8%) from 61.137%
#4623

push

travis-ci

web-flow
docs: add example cases for datatype tests in cases.task (#32379)

138520 of 291367 branches covered (47.54%)

Branch coverage included in aggregate %.

208789 of 283863 relevant lines covered (73.55%)

18031536.81 hits per line

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

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

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
10,269✔
42
  int i = 0;
10,269✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
61,725✔
44
    if (i < 10) {
51,478✔
45
      taosUsleep(1);
46,819✔
46
      i++;
46,799✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
4,659✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
4,659!
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
4,659✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
4,659✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
10,194✔
56
    return false;
95✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
10,099✔
59
  SStmtQNode* node = pStmt->queue.head->next;
10,099✔
60
  pStmt->queue.head = pStmt->queue.head->next;
10,099✔
61
  *param = node;
10,099✔
62

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

65
  return true;
10,182✔
66
}
67

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

72
  pStmt->stat.bindDataNum++;
10,175✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
10,175✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
10,178✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
10,186✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
10,185✔
78
}
10,184✔
79

80
static int32_t stmtCreateRequest(STscStmt* pStmt) {
107,971✔
81
  int32_t code = 0;
107,971✔
82

83
  if (pStmt->exec.pRequest == NULL) {
107,971✔
84
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
11,000✔
85
                        pStmt->reqid);
86
    if (pStmt->reqid != 0) {
11,000!
87
      pStmt->reqid++;
×
88
    }
89
    if (TSDB_CODE_SUCCESS == code) {
11,000!
90
      pStmt->exec.pRequest->syncQuery = true;
11,001✔
91
      pStmt->exec.pRequest->stmtBindVersion = 1;
11,001✔
92
    }
93
  }
94

95
  return code;
107,971✔
96
}
97

98
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
208,842✔
99
  int32_t code = 0;
208,842✔
100

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
208,842!
102
    STMT_LOG_SEQ(newStatus);
209,024!
103
  }
104

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

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

169
  STMT_ERR_RET(code);
212,838!
170

171
  pStmt->sql.status = newStatus;
212,838✔
172

173
  return TSDB_CODE_SUCCESS;
212,838✔
174
}
175

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

179
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
10,078✔
180

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

186
  *tbName = pStmt->bInfo.tbName;
10,079✔
187

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

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

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

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

211
  return TSDB_CODE_SUCCESS;
212
}
213

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

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

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

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

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

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

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

262
  return TSDB_CODE_SUCCESS;
10,908✔
263
}
264

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

268
  pStmt->sql.pVgHash = pVgHash;
10,889✔
269
  pStmt->exec.pBlockHash = pBlockHash;
10,889✔
270

271
  return TSDB_CODE_SUCCESS;
10,889✔
272
}
273

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

279
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
10,895!
280
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
10,910!
281

282
  pStmt->sql.autoCreateTbl = autoCreateTbl;
10,914✔
283
  if (pStmt->sql.autoCreateTbl) {
10,914✔
284
    pStmt->sql.stbInterlaceMode = false;
10,017✔
285
  }
286

287
  return TSDB_CODE_SUCCESS;
10,914✔
288
}
289

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

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

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

299
  return TSDB_CODE_SUCCESS;
×
300
}
301

302
int32_t stmtCacheBlock(STscStmt* pStmt) {
79,882✔
303
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
79,882✔
304
    return TSDB_CODE_SUCCESS;
69,954✔
305
  }
306

307
  uint64_t uid = pStmt->bInfo.tbUid;
9,928✔
308
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
9,928!
309

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

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

320
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
10,015!
321

322
  SStmtTableCache cache = {
10,001✔
323
      .pDataCtx = pDst,
324
      .boundTags = pStmt->bInfo.boundTags,
10,001✔
325
  };
326

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

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

337
  return TSDB_CODE_SUCCESS;
10,016✔
338
}
339

340
int32_t stmtParseSql(STscStmt* pStmt) {
10,901✔
341
  pStmt->exec.pCurrBlock = NULL;
10,901✔
342

343
  SStmtCallback stmtCb = {
10,901✔
344
      .pStmt = pStmt,
345
      .getTbNameFn = stmtGetTbName,
346
      .setInfoFn = stmtUpdateInfo,
347
      .getExecInfoFn = stmtGetExecInfo,
348
  };
349

350
  STMT_ERR_RET(stmtCreateRequest(pStmt));
10,901!
351
  pStmt->exec.pRequest->stmtBindVersion = 1;
10,903✔
352

353
  pStmt->stat.parseSqlNum++;
10,903✔
354
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
10,903✔
355
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
10,903✔
356

357
  pStmt->bInfo.needParse = false;
10,903✔
358

359
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
10,903!
360
    pStmt->sql.type = STMT_TYPE_INSERT;
822✔
361
    pStmt->sql.stbInterlaceMode = false;
822✔
362
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
10,081✔
363
    pStmt->sql.type = STMT_TYPE_QUERY;
2✔
364
    pStmt->sql.stbInterlaceMode = false;
2✔
365

366
    return TSDB_CODE_SUCCESS;
2✔
367
  }
368

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

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

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

385
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
903✔
386
    }
387
  }
388

389
  if (NULL == pStmt->sql.pBindInfo) {
10,919✔
390
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
10,914!
391
    if (NULL == pStmt->sql.pBindInfo) {
10,895!
392
      return terrno;
×
393
    }
394
  }
395

396
  return TSDB_CODE_SUCCESS;
10,900✔
397
}
398

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

407
  pStmt->bInfo.tbName[0] = 0;
26,470✔
408
  pStmt->bInfo.tbFName[0] = 0;
26,470✔
409
  if (!pStmt->bInfo.tagsCached) {
26,470✔
410
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
6,452✔
411
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
6,446!
412
  }
413
  pStmt->bInfo.stbFName[0] = 0;
26,469✔
414

415
  return TSDB_CODE_SUCCESS;
26,469✔
416
}
417

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

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

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

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

443
      if (NULL != pStmt->exec.pCurrBlock) {
77!
444
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
77!
445
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
77✔
446
      }
447
    } else {
448
      pStmt->sql.siInfo.pTableColsIdx = 0;
4,548✔
449
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
4,548✔
450
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
4,552✔
451
    }
452
  } else {
453
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
21,749✔
454
      taos_free_result(pStmt->exec.pRequest);
21,747✔
455
      pStmt->exec.pRequest = NULL;
21,740✔
456
    }
457

458
    size_t keyLen = 0;
21,742✔
459
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
21,742✔
460
    while (pIter) {
43,502✔
461
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
21,732✔
462
      char*          key = taosHashGetKey(pIter, &keyLen);
21,732✔
463
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
21,728✔
464

465
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
21,725✔
466
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
10,871✔
467
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
21,745!
468

469
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
10,857✔
470
        continue;
10,872✔
471
      }
472

473
      qDestroyStmtDataBlock(pBlocks);
10,854✔
474
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
10,851!
475

476
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
10,857✔
477
    }
478

479
    if (keepTable) {
21,770✔
480
      return TSDB_CODE_SUCCESS;
10,874✔
481
    }
482

483
    taosHashCleanup(pStmt->exec.pBlockHash);
10,896✔
484
    pStmt->exec.pBlockHash = NULL;
10,897✔
485

486
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,897✔
487
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,894!
488
  }
489

490
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
15,510!
491

492
  return TSDB_CODE_SUCCESS;
15,513✔
493
}
494

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

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

505
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
10,957✔
506
  STMT_DLOG_E("start to free SQL info");
10,957!
507

508
  taosMemoryFree(pStmt->sql.pBindInfo);
10,957!
509
  taosMemoryFree(pStmt->sql.queryRes.fields);
10,970!
510
  taosMemoryFree(pStmt->sql.queryRes.userFields);
10,968!
511
  taosMemoryFree(pStmt->sql.sqlStr);
10,966!
512
  qDestroyQuery(pStmt->sql.pQuery);
10,971✔
513
  taosArrayDestroy(pStmt->sql.nodeList);
10,970✔
514
  taosHashCleanup(pStmt->sql.pVgHash);
10,967✔
515
  pStmt->sql.pVgHash = NULL;
10,969✔
516

517
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
10,969✔
518
  while (pIter) {
20,987✔
519
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
10,018✔
520

521
    qDestroyStmtDataBlock(pCache->pDataCtx);
10,018✔
522
    qDestroyBoundColInfo(pCache->boundTags);
10,018✔
523
    taosMemoryFreeClear(pCache->boundTags);
10,003!
524

525
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
10,015✔
526
  }
527
  taosHashCleanup(pStmt->sql.pTableCache);
10,969✔
528
  pStmt->sql.pTableCache = NULL;
10,970✔
529

530
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
10,970!
531
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
10,966!
532

533
  taos_free_result(pStmt->sql.siInfo.pRequest);
10,966✔
534
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
10,972✔
535
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
10,972✔
536
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
10,962✔
537
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
10,969✔
538
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
10,971!
539
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
10,964✔
540
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
10,968✔
541

542
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
10,965✔
543
  pStmt->sql.siInfo.tableColsReady = true;
10,965✔
544

545
  STMT_DLOG_E("end to free SQL info");
10,965!
546

547
  return TSDB_CODE_SUCCESS;
10,961✔
548
}
549

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

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

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

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

572
  *vgId = vgInfo.vgId;
9✔
573

574
  return TSDB_CODE_SUCCESS;
9✔
575
}
576

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

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

584
  return TSDB_CODE_SUCCESS;
16✔
585
}
586

587
int32_t stmtGetFromCache(STscStmt* pStmt) {
10,107✔
588
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
10,107!
589
    pStmt->bInfo.needParse = false;
×
590
    pStmt->bInfo.inExecCache = false;
×
591
    return TSDB_CODE_SUCCESS;
×
592
  }
593

594
  pStmt->bInfo.needParse = true;
10,107✔
595
  pStmt->bInfo.inExecCache = false;
10,107✔
596

597
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,107✔
598
  if (pCxtInExec) {
10,126✔
599
    pStmt->bInfo.needParse = false;
16✔
600
    pStmt->bInfo.inExecCache = true;
16✔
601

602
    pStmt->exec.pCurrBlock = *pCxtInExec;
16✔
603

604
    if (pStmt->sql.autoCreateTbl) {
16!
605
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
16!
606
      return TSDB_CODE_SUCCESS;
16✔
607
    }
608
  }
609

610
  if (NULL == pStmt->pCatalog) {
10,110✔
611
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
100!
612
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
100✔
613
  }
614

615
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
10,110✔
616
    if (pStmt->bInfo.inExecCache) {
10,094!
617
      pStmt->bInfo.needParse = false;
×
618
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
619
      return TSDB_CODE_SUCCESS;
×
620
    }
621

622
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
10,094!
623
    return TSDB_CODE_SUCCESS;
10,096✔
624
  }
625

626
  if (pStmt->sql.autoCreateTbl) {
27✔
627
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
8✔
628
    if (pCache) {
8!
629
      pStmt->bInfo.needParse = false;
8✔
630
      pStmt->bInfo.tbUid = 0;
8✔
631

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

635
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
636
                      POINTER_BYTES)) {
637
        STMT_ERR_RET(terrno);
×
638
      }
639

640
      pStmt->exec.pCurrBlock = pNewBlock;
8✔
641

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

644
      return TSDB_CODE_SUCCESS;
8✔
645
    }
646

647
    STMT_RET(stmtCleanBindInfo(pStmt));
×
648
  }
649

650
  uint64_t uid, suid;
651
  int32_t  vgId;
652
  int8_t   tableType;
653

654
  STableMeta*      pTableMeta = NULL;
19✔
655
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
19✔
656
                           .requestId = pStmt->exec.pRequest->requestId,
19✔
657
                           .requestObjRefId = pStmt->exec.pRequest->self,
19✔
658
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
19✔
659
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
8✔
660

661
  pStmt->stat.ctgGetTbMetaNum++;
8✔
662

663
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
8!
664
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
665
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
666

667
    STMT_ERR_RET(code);
×
668
  }
669

670
  STMT_ERR_RET(code);
8!
671

672
  uid = pTableMeta->uid;
8✔
673
  suid = pTableMeta->suid;
8✔
674
  tableType = pTableMeta->tableType;
8✔
675
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
8✔
676
  vgId = pTableMeta->vgId;
8✔
677

678
  taosMemoryFree(pTableMeta);
8!
679

680
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
8!
681

682
  if (uid == pStmt->bInfo.tbUid) {
8!
683
    pStmt->bInfo.needParse = false;
×
684

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

687
    return TSDB_CODE_SUCCESS;
×
688
  }
689

690
  if (pStmt->bInfo.inExecCache) {
8!
691
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
692
    if (NULL == pCache) {
×
693
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
694
               pStmt->bInfo.tbFName, uid, cacheUid);
695

696
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
697
    }
698

699
    pStmt->bInfo.needParse = false;
×
700

701
    pStmt->bInfo.tbUid = uid;
×
702
    pStmt->bInfo.tbSuid = suid;
×
703
    pStmt->bInfo.tbType = tableType;
×
704
    pStmt->bInfo.boundTags = pCache->boundTags;
×
705
    pStmt->bInfo.tagsCached = true;
×
706

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

709
    return TSDB_CODE_SUCCESS;
×
710
  }
711

712
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
8✔
713
  if (pCache) {
8!
714
    pStmt->bInfo.needParse = false;
8✔
715

716
    pStmt->bInfo.tbUid = uid;
8✔
717
    pStmt->bInfo.tbSuid = suid;
8✔
718
    pStmt->bInfo.tbType = tableType;
8✔
719
    pStmt->bInfo.boundTags = pCache->boundTags;
8✔
720
    pStmt->bInfo.tagsCached = true;
8✔
721

722
    STableDataCxt* pNewBlock = NULL;
8✔
723
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
8!
724

725
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
726
                    POINTER_BYTES)) {
727
      STMT_ERR_RET(terrno);
×
728
    }
729

730
    pStmt->exec.pCurrBlock = pNewBlock;
8✔
731

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

734
    return TSDB_CODE_SUCCESS;
8✔
735
  }
736

737
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
738

739
  return TSDB_CODE_SUCCESS;
×
740
}
741

742
int32_t stmtResetStmt(STscStmt* pStmt) {
9,983✔
743
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
9,983!
744

745
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,982✔
746
  if (NULL == pStmt->sql.pTableCache) {
9,990!
747
    STMT_ERR_RET(terrno);
×
748
  }
749

750
  if (pStmt->sql.siInfo.pTableRowDataHash) {
9,990!
751
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
752
  }
753

754
  pStmt->sql.status = STMT_INIT;
9,986✔
755

756
  return TSDB_CODE_SUCCESS;
9,986✔
757
}
758

759
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
10,183✔
760
  SStmtQNode* pParam = (SStmtQNode*)param;
10,183✔
761

762
  if (pParam->restoreTbCols) {
10,183✔
763
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
10,181✔
764
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
5,630✔
765
      *p = taosArrayInit(20, POINTER_BYTES);
5,630✔
766
      if (*p == NULL) {
5,628!
767
        STMT_ERR_RET(terrno);
×
768
      }
769
    }
770

771
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
4,551✔
772
  } else {
773
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
5,629!
774
                                        &pStmt->sql.siInfo));
775

776
    // taosMemoryFree(pParam->pTbData);
777

778
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,628✔
779
  }
780
  return TSDB_CODE_SUCCESS;
10,180✔
781
}
782

783
void* stmtBindThreadFunc(void* param) {
95✔
784
  setThreadName("stmtBind");
95✔
785

786
  qInfo("stmt bind thread started");
95!
787

788
  STscStmt* pStmt = (STscStmt*)param;
95✔
789

790
  while (true) {
10,272✔
791
    if (pStmt->queue.stopQueue) {
10,367✔
792
      break;
95✔
793
    }
794

795
    SStmtQNode* asyncParam = NULL;
10,272✔
796
    if (!stmtDequeue(pStmt, &asyncParam)) {
10,272✔
797
      continue;
95✔
798
    }
799

800
    int ret = stmtAsyncOutput(pStmt, asyncParam);
10,182✔
801
    if (ret != 0) {
10,177!
802
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
803
    }
804
  }
805

806
  qInfo("stmt bind thread stopped");
95!
807

808
  return NULL;
95✔
809
}
810

811
int32_t stmtStartBindThread(STscStmt* pStmt) {
94✔
812
  TdThreadAttr thAttr;
813
  if (taosThreadAttrInit(&thAttr) != 0) {
94!
814
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
815
  }
816
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
95!
817
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
818
  }
819

820
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
94!
821
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
822
    STMT_ERR_RET(terrno);
×
823
  }
824

825
  pStmt->bindThreadInUse = true;
95✔
826

827
  (void)taosThreadAttrDestroy(&thAttr);
95✔
828
  return TSDB_CODE_SUCCESS;
95✔
829
}
830

831
int32_t stmtInitQueue(STscStmt* pStmt) {
94✔
832
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
94✔
833
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
95✔
834
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
190!
835
  pStmt->queue.tail = pStmt->queue.head;
95✔
836

837
  return TSDB_CODE_SUCCESS;
95✔
838
}
839

840
int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
95✔
841
  pTblBuf->buffUnit = sizeof(SStmtQNode);
95✔
842
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
95✔
843
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
95✔
844
  if (NULL == pTblBuf->pBufList) {
95!
845
    return terrno;
×
846
  }
847
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
95!
848
  if (NULL == buff) {
95!
849
    return terrno;
×
850
  }
851

852
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
190!
853
    return terrno;
×
854
  }
855

856
  pTblBuf->pCurBuff = buff;
95✔
857
  pTblBuf->buffIdx = 1;
95✔
858
  pTblBuf->buffOffset = 0;
95✔
859

860
  return TSDB_CODE_SUCCESS;
95✔
861
}
862

863
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid, TAOS_STMT_OPTIONS* pOptions) {
975✔
864
  STscObj*  pObj = (STscObj*)taos;
975✔
865
  STscStmt* pStmt = NULL;
975✔
866
  int32_t   code = 0;
975✔
867

868
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
975!
869
  if (NULL == pStmt) {
977!
870
    return NULL;
×
871
  }
872

873
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
977✔
874
  if (NULL == pStmt->sql.pTableCache) {
976!
875
    taosMemoryFree(pStmt);
×
876
    return NULL;
×
877
  }
878

879
  pStmt->taos = pObj;
976✔
880
  pStmt->bInfo.needParse = true;
976✔
881
  pStmt->sql.status = STMT_INIT;
976✔
882
  pStmt->reqid = reqid;
976✔
883
  pStmt->errCode = TSDB_CODE_SUCCESS;
976✔
884

885
  if (NULL != pOptions) {
976✔
886
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
95✔
887
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
95!
888
      pStmt->stbInterlaceMode = true;
95✔
889
    }
890
  }
891

892
  if (pStmt->stbInterlaceMode) {
976✔
893
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
95✔
894
    pStmt->sql.siInfo.acctId = taos->acctId;
95✔
895
    pStmt->sql.siInfo.dbname = taos->db;
95✔
896
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
95✔
897
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
95✔
898
    if (NULL == pStmt->sql.siInfo.pTableHash) {
95!
899
      (void)stmtClose(pStmt);
×
900
      return NULL;
×
901
    }
902
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
95✔
903
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
95!
904
      STMT_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
905
      (void)stmtClose(pStmt);
×
906
      return NULL;
×
907
    }
908
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
95✔
909
    if (NULL == pStmt->sql.siInfo.pTableCols) {
95!
910
      (void)stmtClose(pStmt);
×
911
      return NULL;
×
912
    }
913

914
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
95✔
915
    if (TSDB_CODE_SUCCESS == code) {
95!
916
      code = stmtInitQueue(pStmt);
95✔
917
    }
918
    if (TSDB_CODE_SUCCESS == code) {
94!
919
      code = stmtStartBindThread(pStmt);
94✔
920
    }
921
    if (TSDB_CODE_SUCCESS != code) {
95!
922
      terrno = code;
×
923
      (void)stmtClose(pStmt);
×
924
      return NULL;
×
925
    }
926
  }
927

928
  pStmt->sql.siInfo.tableColsReady = true;
976✔
929

930
  STMT_LOG_SEQ(STMT_INIT);
976!
931

932
  tscDebug("stmt:%p initialized", pStmt);
976!
933

934
  return pStmt;
976✔
935
}
936

937
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
10,962✔
938
  STscStmt* pStmt = (STscStmt*)stmt;
10,962✔
939

940
  STMT_DLOG_E("start to prepare");
10,962!
941

942
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
10,976!
943
    return pStmt->errCode;
×
944
  }
945

946
  if (pStmt->sql.status >= STMT_PREPARE) {
10,976✔
947
    STMT_ERR_RET(stmtResetStmt(pStmt));
9,997!
948
  }
949

950
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
10,963!
951

952
  if (length <= 0) {
10,967✔
953
    length = strlen(sql);
884✔
954
  }
955

956
  pStmt->sql.sqlStr = taosStrndup(sql, length);
10,967!
957
  if (!pStmt->sql.sqlStr) {
10,973!
958
    return terrno;
×
959
  }
960
  pStmt->sql.sqlLen = length;
10,973✔
961
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
10,973✔
962

963
  char* dbName = NULL;
10,973✔
964
  if (qParseDbName(sql, length, &dbName)) {
10,973✔
965
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
873✔
966
    taosMemoryFreeClear(dbName);
863!
967
  }
968

969
  return TSDB_CODE_SUCCESS;
10,958✔
970
}
971

972
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
77✔
973
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
77✔
974
  if (!pSrc) {
76!
975
    return terrno;
×
976
  }
977
  STableDataCxt* pDst = NULL;
76✔
978

979
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
76!
980
  pStmt->sql.siInfo.pDataCtx = pDst;
77✔
981

982
  SArray* pTblCols = NULL;
77✔
983
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
71,011✔
984
    pTblCols = taosArrayInit(20, POINTER_BYTES);
70,748✔
985
    if (NULL == pTblCols) {
71,172!
986
      return terrno;
×
987
    }
988

989
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
142,106!
990
      return terrno;
×
991
    }
992
  }
993

994
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
263✔
995

996
  return TSDB_CODE_SUCCESS;
263✔
997
}
998

999
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
873✔
1000
  STscStmt* pStmt = (STscStmt*)stmt;
873✔
1001

1002
  STMT_DLOG("start to set dbName:%s", dbName);
873!
1003

1004
  STMT_ERR_RET(stmtCreateRequest(pStmt));
873!
1005

1006
  // The SQL statement specifies a database name, overriding the previously specified database
1007
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
873!
1008
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
873!
1009
  if (pStmt->exec.pRequest->pDb == NULL) {
873!
1010
    return terrno;
×
1011
  }
1012
  return TSDB_CODE_SUCCESS;
873✔
1013
}
1014

1015
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
15,646✔
1016
  STscStmt* pStmt = (STscStmt*)stmt;
15,646✔
1017

1018
  int64_t startUs = taosGetTimestampUs();
15,653✔
1019

1020
  STMT_DLOG("start to set tbName:%s", tbName);
15,653!
1021

1022
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,660!
1023
    return pStmt->errCode;
×
1024
  }
1025

1026
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
15,660!
1027

1028
  int32_t insert = 0;
15,658✔
1029
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
15,658!
1030
  if (0 == insert) {
15,646!
1031
    tscError("set tb name not available for none insert statement");
×
1032
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1033
  }
1034

1035
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
15,646✔
1036
    STMT_ERR_RET(stmtCreateRequest(pStmt));
10,125!
1037

1038
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
10,120!
1039
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1040
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
10,118!
1041

1042
    STMT_ERR_RET(stmtGetFromCache(pStmt));
10,110!
1043

1044
    if (pStmt->bInfo.needParse) {
10,127✔
1045
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
10,096✔
1046
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
10,096✔
1047

1048
      STMT_ERR_RET(stmtParseSql(pStmt));
10,096✔
1049
    }
1050
  } else {
1051
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
5,521✔
1052
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
5,521✔
1053
    pStmt->exec.pRequest->requestId++;
5,521✔
1054
    pStmt->bInfo.needParse = false;
5,521✔
1055
  }
1056

1057
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
15,624✔
1058
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
77!
1059
  }
1060

1061
  int64_t startUs2 = taosGetTimestampUs();
15,632✔
1062
  pStmt->stat.setTbNameUs += startUs2 - startUs;
15,632✔
1063

1064
  return TSDB_CODE_SUCCESS;
15,632✔
1065
}
1066

1067
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
11✔
1068
  STscStmt* pStmt = (STscStmt*)stmt;
11✔
1069

1070
  STMT_DLOG_E("start to set tbTags");
11!
1071

1072
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11!
1073
    return pStmt->errCode;
×
1074
  }
1075

1076
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
11!
1077

1078
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
11✔
1079
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
11!
1080
    tscWarn("no tags bound in sql, will not bound tags");
×
1081
    return TSDB_CODE_SUCCESS;
×
1082
  }
1083

1084
  if (pStmt->bInfo.inExecCache) {
11!
1085
    return TSDB_CODE_SUCCESS;
×
1086
  }
1087

1088
  STableDataCxt** pDataBlock =
1089
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1090
  if (NULL == pDataBlock) {
11!
1091
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1092
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1093
  }
1094

1095
  tscDebug("start to bind stmt tag values");
11!
1096
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
11!
1097
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1098
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1099

1100
  return TSDB_CODE_SUCCESS;
11✔
1101
}
1102

1103
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2✔
1104
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2✔
1105
    return pStmt->errCode;
1✔
1106
  }
1107

1108
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1109
    tscError("invalid operation to get query tag fileds");
×
1110
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1111
  }
1112

1113
  STableDataCxt** pDataBlock =
1114
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1✔
1115
  if (NULL == pDataBlock) {
1!
1116
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1117
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1118
  }
1119

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

1122
  return TSDB_CODE_SUCCESS;
1✔
1123
}
1124

1125
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
6✔
1126
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6!
1127
    return pStmt->errCode;
×
1128
  }
1129

1130
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6!
1131
    tscError("invalid operation to get query column fileds");
×
1132
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1133
  }
1134

1135
  STableDataCxt** pDataBlock = NULL;
6✔
1136

1137
  if (pStmt->sql.stbInterlaceMode) {
6!
1138
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1139
  } else {
1140
    pDataBlock =
1141
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
6✔
1142
    if (NULL == pDataBlock) {
6!
1143
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1144
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1145
    }
1146
  }
1147

1148
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
6!
1149

1150
  return TSDB_CODE_SUCCESS;
6✔
1151
}
1152

1153
/*
1154
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1155
  while (true) {
1156
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1157
      pStmt->exec.smInfo.pColIdx = 0;
1158
    }
1159

1160
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1161
      taosUsleep(1);
1162
      continue;
1163
    }
1164

1165
    *idx = pStmt->exec.smInfo.pColIdx;
1166
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1167
  }
1168
}
1169
*/
1170

1171
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
5,621✔
1172
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,621✔
1173
    pStmt->sql.siInfo.pVgroupHash =
4,555✔
1174
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,552✔
1175
  }
1176
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,624✔
1177
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,555✔
1178
  }
1179

1180
  if (NULL == pStmt->sql.siInfo.pRequest) {
5,622✔
1181
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
77!
1182
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1183

1184
    if (pStmt->reqid != 0) {
77!
1185
      pStmt->reqid++;
×
1186
    }
1187
    pStmt->exec.pRequest->syncQuery = true;
77✔
1188

1189
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
77✔
1190
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
77✔
1191
  }
1192

1193
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
5,622✔
1194
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
372✔
1195
    pStmt->sql.siInfo.tbFromHash = true;
22✔
1196
  }
1197

1198
  if (0 == pStmt->sql.siInfo.firstName[0]) {
5,622✔
1199
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
77✔
1200
  }
1201

1202
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,622✔
1203
  param->next = NULL;
5,622✔
1204

1205
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,622✔
1206

1207
  stmtEnqueue(pStmt, param);
5,631✔
1208

1209
  return TSDB_CODE_SUCCESS;
5,629✔
1210
}
1211

1212
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1213
  while (true) {
1214
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
5,624!
1215
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,619✔
1216
      break;
5,630✔
1217
    } else {
1218
      SArray* pTblCols = NULL;
×
1219
      for (int32_t i = 0; i < 100; i++) {
×
1220
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1221
        if (NULL == pTblCols) {
×
1222
          return terrno;
×
1223
        }
1224

1225
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1226
          return terrno;
×
1227
        }
1228
      }
1229
    }
1230
  }
1231

1232
  return TSDB_CODE_SUCCESS;
5,630✔
1233
}
1234

1235
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
86,240✔
1236
  STscStmt* pStmt = (STscStmt*)stmt;
86,240✔
1237
  int32_t   code = 0;
86,240✔
1238

1239
  int64_t startUs = taosGetTimestampUs();
86,175✔
1240

1241
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
86,175!
1242

1243
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
86,232!
1244
    return pStmt->errCode;
×
1245
  }
1246

1247
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
86,232!
1248

1249
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
86,110!
1250
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1251
    pStmt->bInfo.needParse = false;
×
1252
  }
1253

1254
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
86,110!
1255
    taos_free_result(pStmt->exec.pRequest);
×
1256
    pStmt->exec.pRequest = NULL;
×
1257
  }
1258

1259
  STMT_ERR_RET(stmtCreateRequest(pStmt));
86,110!
1260

1261
  if (pStmt->bInfo.needParse) {
86,087✔
1262
    STMT_ERR_RET(stmtParseSql(pStmt));
824!
1263
  }
1264

1265
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
86,101✔
1266
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
2!
1267

1268
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
2✔
1269
                         .acctId = pStmt->taos->acctId,
2✔
1270
                         .db = pStmt->exec.pRequest->pDb,
2✔
1271
                         .topicQuery = false,
1272
                         .pSql = pStmt->sql.sqlStr,
2✔
1273
                         .sqlLen = pStmt->sql.sqlLen,
2✔
1274
                         .pMsg = pStmt->exec.pRequest->msgBuf,
2✔
1275
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1276
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
2✔
1277
                         .pStmtCb = NULL,
1278
                         .pUser = pStmt->taos->user,
2✔
1279
                         .setQueryFp = setQueryRequest};
1280

1281
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2✔
1282
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
2!
1283

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

1286
    if (pStmt->sql.pQuery->haveResultSet) {
2!
1287
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
2!
1288
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1289
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
2!
1290
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
2!
1291
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
2✔
1292
    }
1293

1294
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
2✔
1295
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
2✔
1296
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
2✔
1297

1298
    return TSDB_CODE_SUCCESS;
2✔
1299
  }
1300

1301
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
86,099!
1302
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1303
  }
1304

1305
  STableDataCxt** pDataBlock = NULL;
86,068✔
1306

1307
  if (pStmt->exec.pCurrBlock) {
86,068✔
1308
    pDataBlock = &pStmt->exec.pCurrBlock;
75,172✔
1309
  } else {
1310
    pDataBlock =
1311
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,896✔
1312
    if (NULL == pDataBlock) {
10,916!
1313
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1314
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1315
    }
1316
    pStmt->exec.pCurrBlock = *pDataBlock;
10,916✔
1317
    if (pStmt->sql.stbInterlaceMode) {
10,916✔
1318
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
77✔
1319
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
77✔
1320
    }
1321
  }
1322

1323
  int64_t startUs2 = taosGetTimestampUs();
86,013✔
1324
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
86,013✔
1325

1326
  SStmtQNode* param = NULL;
86,013✔
1327
  if (pStmt->sql.stbInterlaceMode) {
86,013✔
1328
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,244!
1329
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,254!
1330
    taosArrayClear(param->tblData.aCol);
5,630✔
1331

1332
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1333

1334
    param->restoreTbCols = false;
5,626✔
1335
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,626✔
1336
  }
1337

1338
  int64_t startUs3 = taosGetTimestampUs();
85,930✔
1339
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
85,930✔
1340

1341
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
85,930✔
1342

1343
  if (colIdx < 0) {
85,930!
1344
    if (pStmt->sql.stbInterlaceMode) {
86,099✔
1345
      (*pDataBlock)->pData->flags = 0;
5,630✔
1346
      code =
1347
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
5,630✔
1348
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
5,630✔
1349
    } else {
1350
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
80,469✔
1351
                                pStmt->taos->optionInfo.charsetCxt);
80,469✔
1352
    }
1353

1354
    if (code) {
86,224✔
1355
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1356
      STMT_ERR_RET(code);
1!
1357
    }
1358
  } else {
1359
    if (pStmt->sql.stbInterlaceMode) {
×
1360
      tscError("bind single column not allowed in stb insert mode");
×
1361
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1362
    }
1363

1364
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1365
      tscError("bind column index not in sequence");
×
1366
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1367
    }
1368

1369
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1370

1371
    if (0 == colIdx) {
×
1372
      pStmt->bInfo.sBindRowNum = bind->num;
×
1373
    }
1374

1375
    code =
1376
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
1377
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1378
    if (code) {
×
1379
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1380
      STMT_ERR_RET(code);
×
1381
    }
1382
  }
1383

1384
  int64_t startUs4 = taosGetTimestampUs();
86,082✔
1385
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
86,082✔
1386

1387
  if (pStmt->sql.stbInterlaceMode) {
86,082✔
1388
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,627!
1389
  }
1390

1391
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
86,278✔
1392

1393
  return TSDB_CODE_SUCCESS;
86,278✔
1394
}
1395

1396
int stmtAddBatch(TAOS_STMT* stmt) {
84,666✔
1397
  STscStmt* pStmt = (STscStmt*)stmt;
84,666✔
1398

1399
  int64_t startUs = taosGetTimestampUs();
84,619✔
1400

1401
  STMT_DLOG_E("start to add batch");
84,619!
1402

1403
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
84,557!
1404
    return pStmt->errCode;
×
1405
  }
1406

1407
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
84,557!
1408

1409
  if (pStmt->sql.stbInterlaceMode) {
84,536✔
1410
    int64_t startUs2 = taosGetTimestampUs();
4,552✔
1411
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,552✔
1412

1413
    pStmt->sql.siInfo.tableColsReady = false;
4,552✔
1414

1415
    SStmtQNode* param = NULL;
4,552✔
1416
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
9,103!
1417
    param->restoreTbCols = true;
4,551✔
1418
    param->next = NULL;
4,551✔
1419

1420
    stmtEnqueue(pStmt, param);
4,551✔
1421

1422
    return TSDB_CODE_SUCCESS;
4,555✔
1423
  }
1424

1425
  STMT_ERR_RET(stmtCacheBlock(pStmt));
79,984!
1426

1427
  return TSDB_CODE_SUCCESS;
80,003✔
1428
}
1429
/*
1430
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1431
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1432

1433
  int32_t code = 0;
1434
  int32_t finalCode = 0;
1435
  size_t  keyLen = 0;
1436
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1437
  while (pIter) {
1438
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1439
    char*          key = taosHashGetKey(pIter, &keyLen);
1440

1441
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1442
    if (pMeta->uid) {
1443
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1444
      continue;
1445
    }
1446

1447
    SSubmitBlkRsp* blkRsp = NULL;
1448
    int32_t        i = 0;
1449
    for (; i < pRsp->nBlocks; ++i) {
1450
      blkRsp = pRsp->pBlocks + i;
1451
      if (strlen(blkRsp->tblFName) != keyLen) {
1452
        continue;
1453
      }
1454

1455
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1456
        continue;
1457
      }
1458

1459
      break;
1460
    }
1461

1462
    if (i < pRsp->nBlocks) {
1463
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1464
               blkRsp->uid);
1465

1466
      pMeta->uid = blkRsp->uid;
1467
      pStmt->bInfo.tbUid = blkRsp->uid;
1468
    } else {
1469
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1470
      if (NULL == pStmt->pCatalog) {
1471
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1472
        if (code) {
1473
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1474
          finalCode = code;
1475
          continue;
1476
        }
1477
      }
1478

1479
      code = stmtCreateRequest(pStmt);
1480
      if (code) {
1481
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1482
        finalCode = code;
1483
        continue;
1484
      }
1485

1486
      STableMeta*      pTableMeta = NULL;
1487
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1488
                               .requestId = pStmt->exec.pRequest->requestId,
1489
                               .requestObjRefId = pStmt->exec.pRequest->self,
1490
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1491
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1492

1493
      pStmt->stat.ctgGetTbMetaNum++;
1494

1495
      taos_free_result(pStmt->exec.pRequest);
1496
      pStmt->exec.pRequest = NULL;
1497

1498
      if (code || NULL == pTableMeta) {
1499
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1500
        finalCode = code;
1501
        taosMemoryFree(pTableMeta);
1502
        continue;
1503
      }
1504

1505
      pMeta->uid = pTableMeta->uid;
1506
      pStmt->bInfo.tbUid = pTableMeta->uid;
1507
      taosMemoryFree(pTableMeta);
1508
    }
1509

1510
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1511
  }
1512

1513
  return finalCode;
1514
}
1515
*/
1516

1517
/*
1518
int stmtStaticModeExec(TAOS_STMT* stmt) {
1519
  STscStmt*   pStmt = (STscStmt*)stmt;
1520
  int32_t     code = 0;
1521
  SSubmitRsp* pRsp = NULL;
1522
  if (pStmt->sql.staticMode) {
1523
    return TSDB_CODE_TSC_STMT_API_ERROR;
1524
  }
1525

1526
  STMT_DLOG_E("start to exec");
1527

1528
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1529

1530
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1531
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1532

1533
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1534

1535
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1536
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1537
    if (code) {
1538
      pStmt->exec.pRequest->code = code;
1539
    } else {
1540
      tFreeSSubmitRsp(pRsp);
1541
      STMT_ERR_RET(stmtResetStmt(pStmt));
1542
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1543
    }
1544
  }
1545

1546
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1547

1548
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1549
  pStmt->affectedRows += pStmt->exec.affectedRows;
1550

1551
_return:
1552

1553
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1554

1555
  tFreeSSubmitRsp(pRsp);
1556

1557
  ++pStmt->sql.runTimes;
1558

1559
  STMT_RET(code);
1560
}
1561
*/
1562

1563
int stmtExec(TAOS_STMT* stmt) {
15,400✔
1564
  STscStmt*   pStmt = (STscStmt*)stmt;
15,400✔
1565
  int32_t     code = 0;
15,400✔
1566
  SSubmitRsp* pRsp = NULL;
15,400✔
1567

1568
  int64_t startUs = taosGetTimestampUs();
15,408✔
1569

1570
  STMT_DLOG_E("start to exec");
15,408!
1571

1572
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,416!
1573
    return pStmt->errCode;
×
1574
  }
1575

1576
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
15,416✔
1577

1578
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,400✔
1579
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2✔
1580
  } else {
1581
    if (pStmt->sql.stbInterlaceMode) {
15,398✔
1582
      int64_t startTs = taosGetTimestampUs();
4,554✔
1583
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
10,912✔
1584
        taosUsleep(1);
6,352✔
1585
      }
1586
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,552✔
1587

1588
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,552!
1589
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,553✔
1590
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,554✔
1591
      pStmt->sql.siInfo.pVgroupList = NULL;
4,554✔
1592
    } else {
1593
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,844✔
1594
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,851!
1595

1596
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
10,851!
1597

1598
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
10,856!
1599
    }
1600

1601
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
15,395✔
1602
  }
1603

1604
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
15,421!
1605
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1606
    if (code) {
×
1607
      pStmt->exec.pRequest->code = code;
×
1608
    } else {
1609
      tFreeSSubmitRsp(pRsp);
×
1610
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1611
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1612
    }
1613
  }
1614

1615
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
15,424!
1616

1617
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
15,424✔
1618
  pStmt->affectedRows += pStmt->exec.affectedRows;
15,427✔
1619

1620
_return:
15,427✔
1621

1622
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
15,427!
1623
    taosUsleep(1);
×
1624
  }
1625

1626
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
15,424!
1627

1628
  tFreeSSubmitRsp(pRsp);
15,422✔
1629

1630
  ++pStmt->sql.runTimes;
15,418✔
1631

1632
  int64_t startUs2 = taosGetTimestampUs();
15,422✔
1633
  pStmt->stat.execUseUs += startUs2 - startUs;
15,422✔
1634

1635
  STMT_RET(code);
15,422!
1636
}
1637

1638
int stmtClose(TAOS_STMT* stmt) {
973✔
1639
  STscStmt* pStmt = (STscStmt*)stmt;
973✔
1640

1641
  STMT_DLOG_E("start to free stmt");
973!
1642

1643
  if (pStmt->bindThreadInUse) {
973✔
1644
    pStmt->queue.stopQueue = true;
95✔
1645

1646
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
95✔
1647
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
95✔
1648
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
95✔
1649
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
95✔
1650

1651
    (void)taosThreadJoin(pStmt->bindThread, NULL);
95✔
1652
    pStmt->bindThreadInUse = false;
95✔
1653

1654
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
95✔
1655
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
95✔
1656
  }
1657

1658
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
973!
1659
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1660
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1661
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1662
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1663
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1664
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1665
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1666
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1667
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1668

1669
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
973!
1670
  taosMemoryFree(stmt);
972!
1671

1672
  return TSDB_CODE_SUCCESS;
974✔
1673
}
1674

1675
const char* stmtErrstr(TAOS_STMT* stmt) {
×
1676
  STscStmt* pStmt = (STscStmt*)stmt;
×
1677

1678
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
1679
    return (char*)tstrerror(terrno);
×
1680
  }
1681

1682
  pStmt->exec.pRequest->code = terrno;
×
1683

1684
  return taos_errstr(pStmt->exec.pRequest);
×
1685
}
1686

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

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

1691
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
101,932✔
1692
  STscStmt* pStmt = (STscStmt*)stmt;
101,932✔
1693

1694
  STMT_DLOG_E("start is insert");
101,932!
1695

1696
  if (pStmt->sql.type) {
102,027✔
1697
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
91,112!
1698
  } else {
1699
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
10,915✔
1700
  }
1701

1702
  return TSDB_CODE_SUCCESS;
102,031✔
1703
}
1704

1705
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
4✔
1706
  int32_t   code = 0;
4✔
1707
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1708
  int32_t   preCode = pStmt->errCode;
4✔
1709

1710
  STMT_DLOG_E("start to get tag fields");
4!
1711

1712
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1713
    return pStmt->errCode;
×
1714
  }
1715

1716
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1717
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1718
  }
1719

1720
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1721

1722
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1723
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1724
    pStmt->bInfo.needParse = false;
×
1725
  }
1726

1727
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1728
    taos_free_result(pStmt->exec.pRequest);
×
1729
    pStmt->exec.pRequest = NULL;
×
1730
  }
1731

1732
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1733

1734
  if (pStmt->bInfo.needParse) {
4✔
1735
    STMT_ERRI_JRET(stmtParseSql(pStmt));
3✔
1736
  }
1737

1738
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
2✔
1739

1740
_return:
1✔
1741
  // compatible with previous versions
1742
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
4!
1743
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
1744
  }
1745

1746
  if (code != TSDB_CODE_SUCCESS) {
4✔
1747
    taos_free_result(pStmt->exec.pRequest);
3✔
1748
    pStmt->exec.pRequest = NULL;
3✔
1749
  }
1750
  pStmt->errCode = preCode;
4✔
1751

1752
  return code;
4✔
1753
}
1754

1755
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
1✔
1756
  int32_t   code = 0;
1✔
1757
  STscStmt* pStmt = (STscStmt*)stmt;
1✔
1758
  int32_t   preCode = pStmt->errCode;
1✔
1759

1760
  STMT_DLOG_E("start to get col fields");
1!
1761

1762
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1!
1763
    return pStmt->errCode;
×
1764
  }
1765

1766
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1767
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1768
  }
1769

1770
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
1!
1771

1772
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1!
1773
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1774
    pStmt->bInfo.needParse = false;
×
1775
  }
1776

1777
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1!
1778
    taos_free_result(pStmt->exec.pRequest);
×
1779
    pStmt->exec.pRequest = NULL;
×
1780
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1781
  }
1782

1783
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
1!
1784

1785
  if (pStmt->bInfo.needParse) {
1!
1786
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1787
  }
1788

1789
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
1!
1790

1791
_return:
1✔
1792

1793
  if (code != TSDB_CODE_SUCCESS) {
1!
1794
    taos_free_result(pStmt->exec.pRequest);
×
1795
    pStmt->exec.pRequest = NULL;
×
1796
  }
1797

1798
  pStmt->errCode = preCode;
1✔
1799

1800
  return code;
1✔
1801
}
1802

1803
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
2✔
1804
  int       code = 0;
2✔
1805
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1806
  int32_t   preCode = pStmt->errCode;
2✔
1807

1808
  STMT_DLOG_E("start to get param num");
2!
1809

1810
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2!
1811
    return pStmt->errCode;
×
1812
  }
1813

1814
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2!
1815

1816
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2!
1817
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1818
    pStmt->bInfo.needParse = false;
×
1819
  }
1820

1821
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2!
1822
    taos_free_result(pStmt->exec.pRequest);
×
1823
    pStmt->exec.pRequest = NULL;
×
1824
  }
1825

1826
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2!
1827

1828
  if (pStmt->bInfo.needParse) {
2✔
1829
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1830
  }
1831

1832
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1833
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1834
  } else {
1835
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1836
  }
1837

1838
_return:
1✔
1839

1840
  if (code != TSDB_CODE_SUCCESS) {
2✔
1841
    taos_free_result(pStmt->exec.pRequest);
1✔
1842
    pStmt->exec.pRequest = NULL;
1✔
1843
  }
1844

1845
  pStmt->errCode = preCode;
2✔
1846

1847
  return code;
2✔
1848
}
1849

1850
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
4✔
1851
  int       code = 0;
4✔
1852
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1853
  int32_t   preCode = pStmt->errCode;
4✔
1854

1855
  STMT_DLOG_E("start to get param");
4!
1856

1857
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1858
    return pStmt->errCode;
×
1859
  }
1860

1861
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1862
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1863
  }
1864

1865
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1866

1867
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1868
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1869
    pStmt->bInfo.needParse = false;
×
1870
  }
1871

1872
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1873
    taos_free_result(pStmt->exec.pRequest);
×
1874
    pStmt->exec.pRequest = NULL;
×
1875
  }
1876

1877
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1878

1879
  if (pStmt->bInfo.needParse) {
4!
1880
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1881
  }
1882

1883
  int32_t       nums = 0;
4✔
1884
  TAOS_FIELD_E* pField = NULL;
4✔
1885
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
4!
1886
  if (idx >= nums) {
4!
1887
    tscError("idx %d is too big", idx);
×
1888
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1889
  }
1890

1891
  *type = pField[idx].type;
4✔
1892
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
4✔
1893

1894
_return:
4✔
1895

1896
  if (code != TSDB_CODE_SUCCESS) {
4!
1897
    taos_free_result(pStmt->exec.pRequest);
×
1898
    pStmt->exec.pRequest = NULL;
×
1899
  }
1900

1901
  taosMemoryFree(pField);
4!
1902
  pStmt->errCode = preCode;
4✔
1903

1904
  return code;
4✔
1905
}
1906

1907
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
2✔
1908
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1909

1910
  STMT_DLOG_E("start to use result");
2!
1911

1912
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
2!
1913
    tscError("useResult only for query statement");
×
1914
    return NULL;
×
1915
  }
1916

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