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

taosdata / TDengine / #4548

22 Jul 2025 02:37AM UTC coverage: 54.273% (-3.0%) from 57.287%
#4548

push

travis-ci

GitHub
Merge pull request #32061 from taosdata/new_testcases

132738 of 315239 branches covered (42.11%)

Branch coverage included in aggregate %.

201371 of 300373 relevant lines covered (67.04%)

3475977.14 hits per line

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

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

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
10,264✔
42
  int i = 0;
10,264✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
61,577✔
44
    if (i < 10) {
51,334✔
45
      taosUsleep(1);
46,682✔
46
      i++;
46,662✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
4,652✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
4,651!
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
4,650✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
4,652✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
10,217✔
56
    return false;
85✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
10,132✔
59
  SStmtQNode* node = pStmt->queue.head->next;
10,132✔
60
  pStmt->queue.head = pStmt->queue.head->next;
10,132✔
61
  *param = node;
10,132✔
62

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

65
  return true;
10,183✔
66
}
67

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

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

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

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

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

95
  return code;
49,615✔
96
}
97

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

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
105,790!
102
    STMT_LOG_SEQ(newStatus);
105,870!
103
  }
104

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

110
  switch (newStatus) {
106,979!
111
    case STMT_PREPARE:
650✔
112
      pStmt->errCode = 0;
650✔
113
      break;
650✔
114
    case STMT_SETTBNAME:
5,656✔
115
      if (STMT_STATUS_EQ(INIT)) {
5,656!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
5,656!
119
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
120
      }
121
      break;
5,656✔
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:
48,358✔
133
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
48,358!
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;
48,358✔
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:
47,190✔
148
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
47,190!
149
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
      }
151
      break;
47,190✔
152
    case STMT_EXECUTE:
5,103✔
153
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
5,103✔
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)) {
5,101!
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      }
163
      break;
5,103✔
164
    default:
×
165
      code = TSDB_CODE_APP_ERROR;
×
166
      break;
×
167
  }
168

169
  STMT_ERR_RET(code);
106,979!
170

171
  pStmt->sql.status = newStatus;
106,979✔
172

173
  return TSDB_CODE_SUCCESS;
106,979✔
174
}
175

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

179
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
99✔
180

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

186
  *tbName = pStmt->bInfo.tbName;
98✔
187

188
  return TSDB_CODE_SUCCESS;
98✔
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,
591✔
241
                           bool autoCreateTbl, uint8_t tbNameFlag) {
242
  STscStmt* pStmt = (STscStmt*)stmt;
591✔
243
  char      tbFName[TSDB_TABLE_FNAME_LEN];
244
  int32_t   code = tNameExtractFullName(tbName, tbFName);
591✔
245
  if (code != 0) {
595!
246
    return code;
×
247
  }
248

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

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

262
  return TSDB_CODE_SUCCESS;
595✔
263
}
264

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

268
  pStmt->sql.pVgHash = pVgHash;
590✔
269
  pStmt->exec.pBlockHash = pBlockHash;
590✔
270

271
  return TSDB_CODE_SUCCESS;
590✔
272
}
273

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

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

282
  pStmt->sql.autoCreateTbl = autoCreateTbl;
591✔
283
  if (pStmt->sql.autoCreateTbl) {
591✔
284
    pStmt->sql.stbInterlaceMode = false;
21✔
285
  }
286

287
  return TSDB_CODE_SUCCESS;
591✔
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) {
42,562✔
303
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
42,562✔
304
    return TSDB_CODE_SUCCESS;
42,562✔
305
  }
306

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

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

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

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

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

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

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

337
  return TSDB_CODE_SUCCESS;
20✔
338
}
339

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

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

350
  STMT_ERR_RET(stmtCreateRequest(pStmt));
600!
351

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

356
  pStmt->bInfo.needParse = false;
597✔
357

358
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
597✔
359
    pStmt->sql.type = STMT_TYPE_INSERT;
496✔
360
    pStmt->sql.stbInterlaceMode = false;
496✔
361
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
101✔
362
    pStmt->sql.type = STMT_TYPE_QUERY;
2✔
363
    pStmt->sql.stbInterlaceMode = false;
2✔
364

365
    return TSDB_CODE_SUCCESS;
2✔
366
  }
367

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

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

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

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

388
  if (NULL == pStmt->sql.pBindInfo) {
595✔
389
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
594!
390
    if (NULL == pStmt->sql.pBindInfo) {
593!
391
      return terrno;
×
392
    }
393
  }
394

395
  return TSDB_CODE_SUCCESS;
594✔
396
}
397

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

406
  pStmt->bInfo.tbName[0] = 0;
5,847✔
407
  pStmt->bInfo.tbFName[0] = 0;
5,847✔
408
  if (!pStmt->bInfo.tagsCached) {
5,847✔
409
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
5,807✔
410
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
5,805!
411
  }
412
  pStmt->bInfo.stbFName[0] = 0;
5,842✔
413

414
  return TSDB_CODE_SUCCESS;
5,842✔
415
}
416

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

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

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

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

442
      if (NULL != pStmt->exec.pCurrBlock) {
77!
443
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
77!
444
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
77✔
445
      }
446
    } else {
447
      pStmt->sql.siInfo.pTableColsIdx = 0;
4,549✔
448
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
4,549✔
449
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
4,553✔
450
    }
451
  } else {
452
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
1,111✔
453
      taos_free_result(pStmt->exec.pRequest);
1,109✔
454
      pStmt->exec.pRequest = NULL;
1,118✔
455
    }
456

457
    size_t keyLen = 0;
1,120✔
458
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1,120✔
459
    while (pIter) {
2,200✔
460
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
1,081✔
461
      char*          key = taosHashGetKey(pIter, &keyLen);
1,081✔
462
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
1,081✔
463

464
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
1,082✔
465
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
548✔
466
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
1,097!
467

468
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
546✔
469
        continue;
547✔
470
      }
471

472
      qDestroyStmtDataBlock(pBlocks);
534✔
473
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
533!
474

475
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
533✔
476
    }
477

478
    if (keepTable) {
1,119✔
479
      return TSDB_CODE_SUCCESS;
549✔
480
    }
481

482
    taosHashCleanup(pStmt->exec.pBlockHash);
570✔
483
    pStmt->exec.pBlockHash = NULL;
572✔
484

485
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
572✔
486
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
572!
487
  }
488

489
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
5,201!
490

491
  return TSDB_CODE_SUCCESS;
5,197✔
492
}
493

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

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

504
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
646✔
505
  STMT_DLOG_E("start to free SQL info");
646!
506

507
  taosMemoryFree(pStmt->sql.pBindInfo);
646!
508
  taosMemoryFree(pStmt->sql.queryRes.fields);
648!
509
  taosMemoryFree(pStmt->sql.queryRes.userFields);
649!
510
  taosMemoryFree(pStmt->sql.sqlStr);
649!
511
  qDestroyQuery(pStmt->sql.pQuery);
648✔
512
  taosArrayDestroy(pStmt->sql.nodeList);
648✔
513
  taosHashCleanup(pStmt->sql.pVgHash);
648✔
514
  pStmt->sql.pVgHash = NULL;
649✔
515

516
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
649✔
517
  while (pIter) {
668✔
518
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
20✔
519

520
    qDestroyStmtDataBlock(pCache->pDataCtx);
20✔
521
    qDestroyBoundColInfo(pCache->boundTags);
20✔
522
    taosMemoryFreeClear(pCache->boundTags);
20!
523

524
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
20✔
525
  }
526
  taosHashCleanup(pStmt->sql.pTableCache);
648✔
527
  pStmt->sql.pTableCache = NULL;
649✔
528

529
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
649!
530
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
649!
531

532
  taos_free_result(pStmt->sql.siInfo.pRequest);
649✔
533
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
648✔
534
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
649✔
535
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
649✔
536
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
649✔
537
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
649!
538
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
649✔
539
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
649✔
540

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

544
  STMT_DLOG_E("end to free SQL info");
649!
545

546
  return TSDB_CODE_SUCCESS;
649✔
547
}
548

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

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

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

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

571
  *vgId = vgInfo.vgId;
9✔
572

573
  return TSDB_CODE_SUCCESS;
9✔
574
}
575

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

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

583
  return TSDB_CODE_SUCCESS;
16✔
584
}
585

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

593
  pStmt->bInfo.needParse = true;
129✔
594
  pStmt->bInfo.inExecCache = false;
129✔
595

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

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

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

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

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

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

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

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

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

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

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

643
      return TSDB_CODE_SUCCESS;
8✔
644
    }
645

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

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

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

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

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

666
    STMT_ERR_RET(code);
×
667
  }
668

669
  STMT_ERR_RET(code);
8!
670

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

677
  taosMemoryFree(pTableMeta);
8!
678

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

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

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

686
    return TSDB_CODE_SUCCESS;
×
687
  }
688

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

695
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
696
    }
697

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

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

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

708
    return TSDB_CODE_SUCCESS;
×
709
  }
710

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

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

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

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

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

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

733
    return TSDB_CODE_SUCCESS;
8✔
734
  }
735

736
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
737

738
  return TSDB_CODE_SUCCESS;
×
739
}
740

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

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

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

753
  pStmt->sql.status = STMT_INIT;
9✔
754

755
  return TSDB_CODE_SUCCESS;
9✔
756
}
757

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

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

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

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

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

782
void* stmtBindThreadFunc(void* param) {
85✔
783
  setThreadName("stmtBind");
85✔
784

785
  qInfo("stmt bind thread started");
85!
786

787
  STscStmt* pStmt = (STscStmt*)param;
85✔
788

789
  while (true) {
10,269✔
790
    if (pStmt->queue.stopQueue) {
10,354✔
791
      break;
85✔
792
    }
793

794
    SStmtQNode* asyncParam = NULL;
10,269✔
795
    if (!stmtDequeue(pStmt, &asyncParam)) {
10,269✔
796
      continue;
85✔
797
    }
798

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

805
  qInfo("stmt bind thread stopped");
85!
806

807
  return NULL;
85✔
808
}
809

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

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

824
  pStmt->bindThreadInUse = true;
85✔
825

826
  (void)taosThreadAttrDestroy(&thAttr);
85✔
827
  return TSDB_CODE_SUCCESS;
85✔
828
}
829

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

836
  return TSDB_CODE_SUCCESS;
85✔
837
}
838

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

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

855
  pTblBuf->pCurBuff = buff;
85✔
856
  pTblBuf->buffIdx = 1;
85✔
857
  pTblBuf->buffOffset = 0;
85✔
858

859
  return TSDB_CODE_SUCCESS;
85✔
860
}
861

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

867
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
639!
868
  if (NULL == pStmt) {
641!
869
    return NULL;
×
870
  }
871

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

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

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

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

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

927
  pStmt->sql.siInfo.tableColsReady = true;
641✔
928

929
  STMT_LOG_SEQ(STMT_INIT);
641!
930

931
  tscDebug("stmt:%p initialized", pStmt);
641!
932

933
  return pStmt;
641✔
934
}
935

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

939
  STMT_DLOG_E("start to prepare");
650!
940

941
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
650!
942
    return pStmt->errCode;
×
943
  }
944

945
  if (pStmt->sql.status >= STMT_PREPARE) {
650✔
946
    STMT_ERR_RET(stmtResetStmt(pStmt));
9!
947
  }
948

949
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
650!
950

951
  if (length <= 0) {
650✔
952
    length = strlen(sql);
558✔
953
  }
954

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

962
  char* dbName = NULL;
650✔
963
  if (qParseDbName(sql, length, &dbName)) {
650✔
964
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
547!
965
    taosMemoryFreeClear(dbName);
547!
966
  }
967

968
  return TSDB_CODE_SUCCESS;
650✔
969
}
970

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

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

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

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

993
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
144✔
994

995
  return TSDB_CODE_SUCCESS;
144✔
996
}
997

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

1001
  STMT_DLOG("start to set dbName:%s", dbName);
547!
1002

1003
  STMT_ERR_RET(stmtCreateRequest(pStmt));
547!
1004

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

1014
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
5,652✔
1015
  STscStmt* pStmt = (STscStmt*)stmt;
5,652✔
1016

1017
  int64_t startUs = taosGetTimestampUs();
5,651✔
1018

1019
  STMT_DLOG("start to set tbName:%s", tbName);
5,651!
1020

1021
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
5,656!
1022
    return pStmt->errCode;
×
1023
  }
1024

1025
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
5,656!
1026

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

1034
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
5,658✔
1035
    STMT_ERR_RET(stmtCreateRequest(pStmt));
131!
1036

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

1041
    STMT_ERR_RET(stmtGetFromCache(pStmt));
130!
1042

1043
    if (pStmt->bInfo.needParse) {
129✔
1044
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
98✔
1045
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
98✔
1046

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

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

1060
  int64_t startUs2 = taosGetTimestampUs();
5,654✔
1061
  pStmt->stat.setTbNameUs += startUs2 - startUs;
5,654✔
1062

1063
  return TSDB_CODE_SUCCESS;
5,654✔
1064
}
1065

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

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

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

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

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

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

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

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

1099
  return TSDB_CODE_SUCCESS;
11✔
1100
}
1101

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

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

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

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

1121
  return TSDB_CODE_SUCCESS;
1✔
1122
}
1123

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

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

1134
  STableDataCxt** pDataBlock = NULL;
6✔
1135

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

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

1149
  return TSDB_CODE_SUCCESS;
6✔
1150
}
1151

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

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

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

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

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

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

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

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

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

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

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

1206
  stmtEnqueue(pStmt, param);
5,630✔
1207

1208
  return TSDB_CODE_SUCCESS;
5,629✔
1209
}
1210

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

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

1231
  return TSDB_CODE_SUCCESS;
5,630✔
1232
}
1233

1234
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
48,387✔
1235
  STscStmt* pStmt = (STscStmt*)stmt;
48,387✔
1236
  int32_t   code = 0;
48,387✔
1237

1238
  int64_t startUs = taosGetTimestampUs();
48,358✔
1239

1240
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
48,358!
1241

1242
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
48,381!
1243
    return pStmt->errCode;
×
1244
  }
1245

1246
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
48,381!
1247

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

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

1258
  STMT_ERR_RET(stmtCreateRequest(pStmt));
48,344!
1259

1260
  if (pStmt->bInfo.needParse) {
48,333✔
1261
    STMT_ERR_RET(stmtParseSql(pStmt));
498!
1262
  }
1263

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

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

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

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

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

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

1297
    return TSDB_CODE_SUCCESS;
2✔
1298
  }
1299

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

1304
  STableDataCxt** pDataBlock = NULL;
48,335✔
1305

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

1322
  int64_t startUs2 = taosGetTimestampUs();
48,312✔
1323
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
48,312✔
1324

1325
  SStmtQNode* param = NULL;
48,312✔
1326
  if (pStmt->sql.stbInterlaceMode) {
48,312✔
1327
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,241!
1328
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,255!
1329
    taosArrayClear(param->tblData.aCol);
5,630✔
1330

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

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

1337
  int64_t startUs3 = taosGetTimestampUs();
48,324✔
1338
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
48,324✔
1339

1340
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
48,324✔
1341

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

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

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

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

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

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

1383
  int64_t startUs4 = taosGetTimestampUs();
48,357✔
1384
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
48,357✔
1385

1386
  if (pStmt->sql.stbInterlaceMode) {
48,357✔
1387
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,630!
1388
  }
1389

1390
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
48,457✔
1391

1392
  return TSDB_CODE_SUCCESS;
48,457✔
1393
}
1394

1395
int stmtAddBatch(TAOS_STMT* stmt) {
47,166✔
1396
  STscStmt* pStmt = (STscStmt*)stmt;
47,166✔
1397

1398
  int64_t startUs = taosGetTimestampUs();
47,171✔
1399

1400
  STMT_DLOG_E("start to add batch");
47,171!
1401

1402
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
47,146!
1403
    return pStmt->errCode;
×
1404
  }
1405

1406
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
47,146!
1407

1408
  if (pStmt->sql.stbInterlaceMode) {
47,182✔
1409
    int64_t startUs2 = taosGetTimestampUs();
4,554✔
1410
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,554✔
1411

1412
    pStmt->sql.siInfo.tableColsReady = false;
4,554✔
1413

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

1419
    stmtEnqueue(pStmt, param);
4,554✔
1420

1421
    return TSDB_CODE_SUCCESS;
4,555✔
1422
  }
1423

1424
  STMT_ERR_RET(stmtCacheBlock(pStmt));
42,627!
1425

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

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

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

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

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

1458
      break;
1459
    }
1460

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

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

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

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

1492
      pStmt->stat.ctgGetTbMetaNum++;
1493

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

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

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

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

1512
  return finalCode;
1513
}
1514
*/
1515

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

1525
  STMT_DLOG_E("start to exec");
1526

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

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

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

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

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

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

1550
_return:
1551

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

1554
  tFreeSSubmitRsp(pRsp);
1555

1556
  ++pStmt->sql.runTimes;
1557

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

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

1567
  int64_t startUs = taosGetTimestampUs();
5,103✔
1568

1569
  STMT_DLOG_E("start to exec");
5,103!
1570

1571
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
5,102!
1572
    return pStmt->errCode;
×
1573
  }
1574

1575
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
5,102✔
1576

1577
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
5,100✔
1578
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2✔
1579
  } else {
1580
    if (pStmt->sql.stbInterlaceMode) {
5,098✔
1581
      int64_t startTs = taosGetTimestampUs();
4,555✔
1582
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
12,788✔
1583
        taosUsleep(1);
8,233✔
1584
      }
1585
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,555✔
1586

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

1595
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
547!
1596

1597
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
547!
1598
    }
1599

1600
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
5,102✔
1601
  }
1602

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

1614
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
5,103!
1615

1616
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
5,103✔
1617
  pStmt->affectedRows += pStmt->exec.affectedRows;
5,103✔
1618

1619
_return:
5,103✔
1620

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

1625
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
5,100!
1626

1627
  tFreeSSubmitRsp(pRsp);
5,096✔
1628

1629
  ++pStmt->sql.runTimes;
5,094✔
1630

1631
  int64_t startUs2 = taosGetTimestampUs();
5,091✔
1632
  pStmt->stat.execUseUs += startUs2 - startUs;
5,091✔
1633

1634
  STMT_RET(code);
5,091!
1635
}
1636

1637
int stmtClose(TAOS_STMT* stmt) {
639✔
1638
  STscStmt* pStmt = (STscStmt*)stmt;
639✔
1639

1640
  STMT_DLOG_E("start to free stmt");
639!
1641

1642
  if (pStmt->bindThreadInUse) {
639✔
1643
    pStmt->queue.stopQueue = true;
85✔
1644

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

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

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

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

1668
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
639!
1669
  taosMemoryFree(stmt);
640!
1670

1671
  return TSDB_CODE_SUCCESS;
640✔
1672
}
1673

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

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

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

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

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

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

1690
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
53,964✔
1691
  STscStmt* pStmt = (STscStmt*)stmt;
53,964✔
1692

1693
  STMT_DLOG_E("start is insert");
53,964!
1694

1695
  if (pStmt->sql.type) {
54,022✔
1696
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
53,429!
1697
  } else {
1698
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
593✔
1699
  }
1700

1701
  return TSDB_CODE_SUCCESS;
54,022✔
1702
}
1703

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

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

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

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

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

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

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

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

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

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

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

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

1751
  return code;
4✔
1752
}
1753

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

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

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

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

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

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

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

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

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

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

1790
_return:
1✔
1791

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

1797
  pStmt->errCode = preCode;
1✔
1798

1799
  return code;
1✔
1800
}
1801

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

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

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

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

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

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

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

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

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

1837
_return:
1✔
1838

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

1844
  pStmt->errCode = preCode;
2✔
1845

1846
  return code;
2✔
1847
}
1848

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

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

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

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

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

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

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

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

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

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

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

1893
_return:
4✔
1894

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

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

1903
  return code;
4✔
1904
}
1905

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

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

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

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