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

taosdata / TDengine / #4416

03 Jul 2025 10:49AM UTC coverage: 61.007% (-1.2%) from 62.241%
#4416

push

travis-ci

GitHub
Merge pull request #31575 from taosdata/fix/huoh/taos_log

150735 of 316232 branches covered (47.67%)

Branch coverage included in aggregate %.

233783 of 314057 relevant lines covered (74.44%)

6782670.15 hits per line

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

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

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

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

65
  return true;
36✔
66
}
67

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

72
  pStmt->stat.bindDataNum++;
36✔
73

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

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

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

95
  return code;
101,766✔
96
}
97

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

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
182,044!
102
    STMT_LOG_SEQ(newStatus);
182,112✔
103
  }
104

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

110
  switch (newStatus) {
182,091!
111
    case STMT_PREPARE:
20,030✔
112
      pStmt->errCode = 0;
20,030✔
113
      break;
20,030✔
114
    case STMT_SETTBNAME:
40,031✔
115
      if (STMT_STATUS_EQ(INIT)) {
40,031!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
40,031!
119
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
120
      }
121
      break;
40,031✔
122
    case STMT_SETTAGS:
21✔
123
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
21!
124
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
125
      }
126
      break;
21✔
127
    case STMT_FETCH_FIELDS:
1,064✔
128
      if (STMT_STATUS_EQ(INIT)) {
1,064!
129
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
130
      }
131
      break;
1,064✔
132
    case STMT_BIND:
40,657✔
133
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
40,657!
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;
40,657✔
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:
40,221✔
148
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
40,221!
149
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
      }
151
      break;
40,221✔
152
    case STMT_EXECUTE:
40,067✔
153
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
40,067✔
154
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
17!
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)) {
40,050!
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      }
163
      break;
40,067✔
164
    default:
×
165
      code = TSDB_CODE_APP_ERROR;
×
166
      break;
×
167
  }
168

169
  STMT_ERR_RET(code);
182,091!
170

171
  pStmt->sql.status = newStatus;
182,091✔
172

173
  return TSDB_CODE_SUCCESS;
182,091✔
174
}
175

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

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

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

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

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

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

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

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

211
  return TSDB_CODE_SUCCESS;
212
}
213

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

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

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

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

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

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

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

262
  return TSDB_CODE_SUCCESS;
20,041✔
263
}
264

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

268
  pStmt->sql.pVgHash = pVgHash;
19,999✔
269
  pStmt->exec.pBlockHash = pBlockHash;
19,999✔
270

271
  return TSDB_CODE_SUCCESS;
19,999✔
272
}
273

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

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

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

286
  return TSDB_CODE_SUCCESS;
20,034✔
287
}
288

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

292
  *pVgHash = pStmt->sql.pVgHash;
18✔
293
  pStmt->sql.pVgHash = NULL;
18✔
294

295
  *pBlockHash = pStmt->exec.pBlockHash;
18✔
296
  pStmt->exec.pBlockHash = NULL;
18✔
297

298
  return TSDB_CODE_SUCCESS;
18✔
299
}
300

301
int32_t stmtCacheBlock(STscStmt* pStmt) {
40,200✔
302
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
40,200✔
303
    return TSDB_CODE_SUCCESS;
30✔
304
  }
305

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

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

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

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

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

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

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

336
  return TSDB_CODE_SUCCESS;
20,029✔
337
}
338

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

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

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

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

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

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

364
    return TSDB_CODE_SUCCESS;
17✔
365
  }
366

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

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

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

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

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

394
  return TSDB_CODE_SUCCESS;
20,029✔
395
}
396

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

405
  pStmt->bInfo.tbName[0] = 0;
40,116✔
406
  pStmt->bInfo.tbFName[0] = 0;
40,116✔
407
  if (!pStmt->bInfo.tagsCached) {
40,116✔
408
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
101✔
409
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
101!
410
  }
411
  pStmt->bInfo.stbFName[0] = 0;
40,118✔
412

413
  return TSDB_CODE_SUCCESS;
40,118✔
414
}
415

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

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

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

435
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
60,106✔
436
  if (pStmt->sql.stbInterlaceMode) {
60,106✔
437
    if (deepClean) {
10✔
438
      taosHashCleanup(pStmt->exec.pBlockHash);
1✔
439
      pStmt->exec.pBlockHash = NULL;
1✔
440

441
      if (NULL != pStmt->exec.pCurrBlock) {
1!
442
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
1!
443
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
1✔
444
      }
445
    } else {
446
      pStmt->sql.siInfo.pTableColsIdx = 0;
9✔
447
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
9✔
448
      tSimpleHashClear(pStmt->sql.siInfo.pTableUidHash);
9✔
449
    }
450
  } else {
451
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
60,096✔
452
      taos_free_result(pStmt->exec.pRequest);
60,079✔
453
      pStmt->exec.pRequest = NULL;
60,068✔
454
    }
455

456
    size_t keyLen = 0;
60,085✔
457
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
60,085✔
458
    while (pIter) {
120,210✔
459
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
60,100✔
460
      char*          key = taosHashGetKey(pIter, &keyLen);
60,100✔
461
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
60,095✔
462

463
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
60,094✔
464
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
40,040✔
465
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
80,102!
466

467
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
40,035✔
468
        continue;
40,045✔
469
      }
470

471
      qDestroyStmtDataBlock(pBlocks);
20,054✔
472
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
20,057!
473

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

477
    if (keepTable) {
60,110✔
478
      return TSDB_CODE_SUCCESS;
40,062✔
479
    }
480

481
    taosHashCleanup(pStmt->exec.pBlockHash);
20,048✔
482
    pStmt->exec.pBlockHash = NULL;
20,049✔
483

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

488
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
20,057!
489

490
  return TSDB_CODE_SUCCESS;
20,053✔
491
}
492

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

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

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

506
  taosMemoryFree(pStmt->sql.pBindInfo);
20,027!
507
  taosMemoryFree(pStmt->sql.queryRes.fields);
20,047!
508
  taosMemoryFree(pStmt->sql.queryRes.userFields);
20,047!
509
  taosMemoryFree(pStmt->sql.sqlStr);
20,045!
510
  qDestroyQuery(pStmt->sql.pQuery);
20,047✔
511
  taosArrayDestroy(pStmt->sql.nodeList);
20,049✔
512
  taosHashCleanup(pStmt->sql.pVgHash);
20,047✔
513
  pStmt->sql.pVgHash = NULL;
20,050✔
514

515
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
20,050✔
516
  while (pIter) {
40,081✔
517
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
20,032✔
518

519
    qDestroyStmtDataBlock(pCache->pDataCtx);
20,032✔
520
    qDestroyBoundColInfo(pCache->boundTags);
20,031✔
521
    taosMemoryFreeClear(pCache->boundTags);
20,031!
522

523
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
20,033✔
524
  }
525
  taosHashCleanup(pStmt->sql.pTableCache);
20,049✔
526
  pStmt->sql.pTableCache = NULL;
20,049✔
527

528
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
20,049!
529
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
20,046!
530

531
  taos_free_result(pStmt->sql.siInfo.pRequest);
20,047✔
532
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
20,046✔
533
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
20,045✔
534
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableUidHash);
20,040✔
535
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
20,047✔
536
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
20,047!
537
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
20,048✔
538
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
20,046✔
539

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

543
  STMT_DLOG_E("end to free SQL info");
20,048✔
544

545
  return TSDB_CODE_SUCCESS;
20,036✔
546
}
547

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

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

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

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

570
  *vgId = vgInfo.vgId;
10✔
571

572
  return TSDB_CODE_SUCCESS;
10✔
573
}
574

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

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

582
  return TSDB_CODE_SUCCESS;
17✔
583
}
584

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

592
  pStmt->bInfo.needParse = true;
39,993✔
593
  pStmt->bInfo.inExecCache = false;
39,993✔
594

595
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
39,993✔
596
  if (pCxtInExec) {
40,040✔
597
    pStmt->bInfo.needParse = false;
20,000✔
598
    pStmt->bInfo.inExecCache = true;
20,000✔
599

600
    pStmt->exec.pCurrBlock = *pCxtInExec;
20,000✔
601

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

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

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

620
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
20,005✔
621
    return TSDB_CODE_SUCCESS;
20,006✔
622
  }
623

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

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

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

638
      pStmt->exec.pCurrBlock = pNewBlock;
9✔
639

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

642
      return TSDB_CODE_SUCCESS;
9✔
643
    }
644

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

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

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

659
  pStmt->stat.ctgGetTbMetaNum++;
26✔
660

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

665
    STMT_ERR_RET(code);
×
666
  }
667

668
  STMT_ERR_RET(code);
26!
669

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

676
  taosMemoryFree(pTableMeta);
26!
677

678
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
26✔
679

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

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

685
    return TSDB_CODE_SUCCESS;
×
686
  }
687

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

694
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
695
    }
696

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

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

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

707
    return TSDB_CODE_SUCCESS;
×
708
  }
709

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

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

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

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

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

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

732
    return TSDB_CODE_SUCCESS;
8✔
733
  }
734

735
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
18!
736

737
  return TSDB_CODE_SUCCESS;
18✔
738
}
739

740
int32_t stmtResetStmt(STscStmt* pStmt) {
19,959✔
741
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
19,959!
742

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

748
  if (pStmt->sql.siInfo.pTableUidHash) {
19,966!
749
    tSimpleHashClear(pStmt->sql.siInfo.pTableUidHash);
×
750
  }
751

752
  pStmt->sql.status = STMT_INIT;
19,966✔
753

754
  return TSDB_CODE_SUCCESS;
19,966✔
755
}
756

757
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
36✔
758
  SStmtQNode* pParam = (SStmtQNode*)param;
36✔
759

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

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

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

776
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
27✔
777
  }
778
  return TSDB_CODE_SUCCESS;
36✔
779
}
780

781
void* stmtBindThreadFunc(void* param) {
21✔
782
  setThreadName("stmtBind");
21✔
783

784
  qInfo("stmt bind thread started");
21!
785

786
  STscStmt* pStmt = (STscStmt*)param;
21✔
787

788
  while (true) {
57✔
789
    if (pStmt->queue.stopQueue) {
78✔
790
      break;
21✔
791
    }
792

793
    SStmtQNode* asyncParam = NULL;
57✔
794
    if (!stmtDequeue(pStmt, &asyncParam)) {
57✔
795
      continue;
21✔
796
    }
797

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

804
  qInfo("stmt bind thread stopped");
21!
805

806
  return NULL;
21✔
807
}
808

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

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

823
  pStmt->bindThreadInUse = true;
21✔
824

825
  (void)taosThreadAttrDestroy(&thAttr);
21✔
826
  return TSDB_CODE_SUCCESS;
21✔
827
}
828

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

835
  return TSDB_CODE_SUCCESS;
21✔
836
}
837

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

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

854
  pTblBuf->pCurBuff = buff;
21✔
855
  pTblBuf->buffIdx = 1;
21✔
856
  pTblBuf->buffOffset = 0;
21✔
857

858
  return TSDB_CODE_SUCCESS;
21✔
859
}
860

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

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

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

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

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

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

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

926
  pStmt->sql.siInfo.tableColsReady = true;
71✔
927

928
  STMT_LOG_SEQ(STMT_INIT);
71✔
929

930
  tscDebug("stmt:%p initialized", pStmt);
71✔
931

932
  return pStmt;
71✔
933
}
934

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

938
  STMT_DLOG_E("start to prepare");
20,030✔
939

940
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,050!
941
    return pStmt->errCode;
×
942
  }
943

944
  if (pStmt->sql.status >= STMT_PREPARE) {
20,050✔
945
    STMT_ERR_RET(stmtResetStmt(pStmt));
19,979!
946
  }
947

948
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
20,028!
949

950
  if (length <= 0) {
20,029✔
951
    length = strlen(sql);
20✔
952
  }
953

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

961
  char* dbName = NULL;
20,032✔
962
  if (qParseDbName(sql, length, &dbName)) {
20,032✔
963
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
3!
964
    taosMemoryFreeClear(dbName);
3!
965
  }
966

967
  return TSDB_CODE_SUCCESS;
20,016✔
968
}
969

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

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

980
  SArray* pTblCols = NULL;
1✔
981
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
1,001✔
982
    pTblCols = taosArrayInit(20, POINTER_BYTES);
1,000✔
983
    if (NULL == pTblCols) {
1,000!
984
      return terrno;
×
985
    }
986

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

992
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
1✔
993

994
  return TSDB_CODE_SUCCESS;
1✔
995
}
996

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

1000
  STMT_DLOG("start to set dbName:%s", dbName);
3!
1001

1002
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3!
1003

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

1013
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
40,001✔
1014
  STscStmt* pStmt = (STscStmt*)stmt;
40,001✔
1015

1016
  int64_t startUs = taosGetTimestampUs();
40,043✔
1017

1018
  STMT_DLOG("start to set tbName:%s", tbName);
40,043✔
1019

1020
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
40,040!
1021
    return pStmt->errCode;
×
1022
  }
1023

1024
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
40,040!
1025

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

1033
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
40,033✔
1034
    STMT_ERR_RET(stmtCreateRequest(pStmt));
40,025!
1035

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

1040
    STMT_ERR_RET(stmtGetFromCache(pStmt));
40,002!
1041

1042
    if (pStmt->bInfo.needParse) {
40,046✔
1043
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
20,032✔
1044
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
20,032✔
1045

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

1055
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
40,022✔
1056
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
1!
1057
  }
1058

1059
  int64_t startUs2 = taosGetTimestampUs();
40,044✔
1060
  pStmt->stat.setTbNameUs += startUs2 - startUs;
40,044✔
1061

1062
  return TSDB_CODE_SUCCESS;
40,044✔
1063
}
1064

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

1068
  STMT_DLOG_E("start to set tbTags");
21✔
1069

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

1074
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
21!
1075

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

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

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

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

1098
  return TSDB_CODE_SUCCESS;
21✔
1099
}
1100

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

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

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

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

1120
  return TSDB_CODE_SUCCESS;
1✔
1121
}
1122

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

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

1133
  STableDataCxt** pDataBlock = NULL;
1,059✔
1134

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

1146
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
1,059!
1147

1148
  return TSDB_CODE_SUCCESS;
1,059✔
1149
}
1150

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

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

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

1169
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
27✔
1170
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
27✔
1171
    pStmt->sql.siInfo.pVgroupHash =
9✔
1172
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
9✔
1173
  }
1174
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
27✔
1175
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
9✔
1176
  }
1177

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

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

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

1191
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
27✔
1192
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
1!
1193
    pStmt->sql.siInfo.tbFromHash = true;
1✔
1194
  }
1195

1196
  if (0 == pStmt->sql.siInfo.firstName[0]) {
27✔
1197
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
1✔
1198
  }
1199

1200
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
27✔
1201
  param->next = NULL;
27✔
1202

1203
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
27✔
1204

1205
  stmtEnqueue(pStmt, param);
27✔
1206

1207
  return TSDB_CODE_SUCCESS;
27✔
1208
}
1209

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

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

1230
  return TSDB_CODE_SUCCESS;
27✔
1231
}
1232

1233
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
40,662✔
1234
  STscStmt* pStmt = (STscStmt*)stmt;
40,662✔
1235
  int32_t   code = 0;
40,662✔
1236

1237
  int64_t startUs = taosGetTimestampUs();
40,665✔
1238

1239
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
40,665✔
1240

1241
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
40,670!
1242
    return pStmt->errCode;
×
1243
  }
1244

1245
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
40,670!
1246

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

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

1257
  STMT_ERR_RET(stmtCreateRequest(pStmt));
40,663!
1258

1259
  if (pStmt->bInfo.needParse) {
40,657✔
1260
    STMT_ERR_RET(stmtParseSql(pStmt));
26✔
1261
  }
1262

1263
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
40,647✔
1264
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
17!
1265

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

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

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

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

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

1296
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1297
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1298
    // }
1299

1300
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1301

1302
    return TSDB_CODE_SUCCESS;
17✔
1303
  }
1304

1305
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
40,630!
1306
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1307
  }
1308

1309
  STableDataCxt** pDataBlock = NULL;
40,627✔
1310

1311
  if (pStmt->exec.pCurrBlock) {
40,627✔
1312
    pDataBlock = &pStmt->exec.pCurrBlock;
20,610✔
1313
  } else {
1314
    pDataBlock =
1315
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,017✔
1316
    if (NULL == pDataBlock) {
20,040!
1317
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1318
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1319
    }
1320
    pStmt->exec.pCurrBlock = *pDataBlock;
20,040✔
1321
    if (pStmt->sql.stbInterlaceMode) {
20,040✔
1322
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
1✔
1323
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
1✔
1324
    }
1325
  }
1326

1327
  int64_t startUs2 = taosGetTimestampUs();
40,649✔
1328
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
40,649✔
1329

1330
  SStmtQNode* param = NULL;
40,649✔
1331
  if (pStmt->sql.stbInterlaceMode) {
40,649✔
1332
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
54!
1333
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
54!
1334
    taosArrayClear(param->tblData.aCol);
27✔
1335

1336
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1337

1338
    param->restoreTbCols = false;
27✔
1339
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
27✔
1340
  }
1341

1342
  int64_t startUs3 = taosGetTimestampUs();
40,640✔
1343
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
40,640✔
1344

1345
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
40,640✔
1346

1347
  if (colIdx < 0) {
40,640✔
1348
    if (pStmt->sql.stbInterlaceMode) {
40,196✔
1349
      (*pDataBlock)->pData->flags = 0;
27✔
1350
      code =
1351
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
27✔
1352
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
27✔
1353
    } else {
1354
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
40,169✔
1355
                                pStmt->taos->optionInfo.charsetCxt);
40,169✔
1356
    }
1357

1358
    if (code) {
40,197✔
1359
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
7!
1360
      STMT_ERR_RET(code);
7!
1361
    }
1362
  } else {
1363
    if (pStmt->sql.stbInterlaceMode) {
444!
1364
      tscError("bind single column not allowed in stb insert mode");
×
1365
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1366
    }
1367

1368
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
444!
1369
      tscError("bind column index not in sequence");
×
1370
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1371
    }
1372

1373
    pStmt->bInfo.sBindLastIdx = colIdx;
444✔
1374

1375
    if (0 == colIdx) {
444✔
1376
      pStmt->bInfo.sBindRowNum = bind->num;
75✔
1377
    }
1378

1379
    code =
1380
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
444✔
1381
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
444✔
1382
    if (code) {
450!
1383
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1384
      STMT_ERR_RET(code);
×
1385
    }
1386
  }
1387

1388
  int64_t startUs4 = taosGetTimestampUs();
40,637✔
1389
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
40,637✔
1390

1391
  if (pStmt->sql.stbInterlaceMode) {
40,637✔
1392
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
27!
1393
  }
1394

1395
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
40,643✔
1396

1397
  return TSDB_CODE_SUCCESS;
40,643✔
1398
}
1399

1400
int stmtAddBatch(TAOS_STMT* stmt) {
40,210✔
1401
  STscStmt* pStmt = (STscStmt*)stmt;
40,210✔
1402

1403
  int64_t startUs = taosGetTimestampUs();
40,222✔
1404

1405
  STMT_DLOG_E("start to add batch");
40,222✔
1406

1407
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
40,214!
1408
    return pStmt->errCode;
×
1409
  }
1410

1411
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
40,214✔
1412

1413
  if (pStmt->sql.stbInterlaceMode) {
40,216✔
1414
    int64_t startUs2 = taosGetTimestampUs();
9✔
1415
    pStmt->stat.addBatchUs += startUs2 - startUs;
9✔
1416

1417
    pStmt->sql.siInfo.tableColsReady = false;
9✔
1418

1419
    SStmtQNode* param = NULL;
9✔
1420
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
18!
1421
    param->restoreTbCols = true;
9✔
1422
    param->next = NULL;
9✔
1423

1424
    stmtEnqueue(pStmt, param);
9✔
1425

1426
    return TSDB_CODE_SUCCESS;
9✔
1427
  }
1428

1429
  STMT_ERR_RET(stmtCacheBlock(pStmt));
40,207!
1430

1431
  return TSDB_CODE_SUCCESS;
40,215✔
1432
}
1433
/*
1434
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1435
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1436

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

1445
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1446
    if (pMeta->uid) {
1447
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1448
      continue;
1449
    }
1450

1451
    SSubmitBlkRsp* blkRsp = NULL;
1452
    int32_t        i = 0;
1453
    for (; i < pRsp->nBlocks; ++i) {
1454
      blkRsp = pRsp->pBlocks + i;
1455
      if (strlen(blkRsp->tblFName) != keyLen) {
1456
        continue;
1457
      }
1458

1459
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1460
        continue;
1461
      }
1462

1463
      break;
1464
    }
1465

1466
    if (i < pRsp->nBlocks) {
1467
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1468
               blkRsp->uid);
1469

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

1483
      code = stmtCreateRequest(pStmt);
1484
      if (code) {
1485
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1486
        finalCode = code;
1487
        continue;
1488
      }
1489

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

1497
      pStmt->stat.ctgGetTbMetaNum++;
1498

1499
      taos_free_result(pStmt->exec.pRequest);
1500
      pStmt->exec.pRequest = NULL;
1501

1502
      if (code || NULL == pTableMeta) {
1503
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1504
        finalCode = code;
1505
        taosMemoryFree(pTableMeta);
1506
        continue;
1507
      }
1508

1509
      pMeta->uid = pTableMeta->uid;
1510
      pStmt->bInfo.tbUid = pTableMeta->uid;
1511
      taosMemoryFree(pTableMeta);
1512
    }
1513

1514
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1515
  }
1516

1517
  return finalCode;
1518
}
1519
*/
1520

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

1530
  STMT_DLOG_E("start to exec");
1531

1532
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1533

1534
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1535
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1536

1537
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1538

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

1550
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1551

1552
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1553
  pStmt->affectedRows += pStmt->exec.affectedRows;
1554

1555
_return:
1556

1557
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1558

1559
  tFreeSSubmitRsp(pRsp);
1560

1561
  ++pStmt->sql.runTimes;
1562

1563
  STMT_RET(code);
1564
}
1565
*/
1566

1567
int stmtExec(TAOS_STMT* stmt) {
40,036✔
1568
  STscStmt*   pStmt = (STscStmt*)stmt;
40,036✔
1569
  int32_t     code = 0;
40,036✔
1570
  SSubmitRsp* pRsp = NULL;
40,036✔
1571

1572
  int64_t startUs = taosGetTimestampUs();
40,056✔
1573

1574
  STMT_DLOG_E("start to exec");
40,056✔
1575

1576
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
40,057!
1577
    return pStmt->errCode;
×
1578
  }
1579

1580
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
40,057✔
1581

1582
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
40,039✔
1583
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
17✔
1584
  } else {
1585
    if (pStmt->sql.stbInterlaceMode) {
40,022✔
1586
      int64_t startTs = taosGetTimestampUs();
9✔
1587
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
20✔
1588
        taosUsleep(1);
11✔
1589
      }
1590
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
9✔
1591

1592
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
9!
1593
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
9✔
1594
      pStmt->sql.siInfo.pVgroupHash = NULL;
9✔
1595
      pStmt->sql.siInfo.pVgroupList = NULL;
9✔
1596
    } else {
1597
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
40,013✔
1598
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
40,025!
1599

1600
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
40,026!
1601

1602
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
40,026!
1603
    }
1604

1605
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
40,016✔
1606
  }
1607

1608
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
40,066!
1609
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1610
    if (code) {
×
1611
      pStmt->exec.pRequest->code = code;
×
1612
    } else {
1613
      tFreeSSubmitRsp(pRsp);
×
1614
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1615
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1616
    }
1617
  }
1618

1619
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
40,071✔
1620

1621
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
40,070✔
1622
  pStmt->affectedRows += pStmt->exec.affectedRows;
40,072✔
1623

1624
_return:
40,073✔
1625

1626
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
40,073!
1627
    taosUsleep(1);
×
1628
  }
1629

1630
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
40,072!
1631

1632
  tFreeSSubmitRsp(pRsp);
40,071✔
1633

1634
  ++pStmt->sql.runTimes;
40,070✔
1635

1636
  int64_t startUs2 = taosGetTimestampUs();
40,071✔
1637
  pStmt->stat.execUseUs += startUs2 - startUs;
40,071✔
1638

1639
  STMT_RET(code);
40,071✔
1640
}
1641

1642
int stmtClose(TAOS_STMT* stmt) {
69✔
1643
  STscStmt* pStmt = (STscStmt*)stmt;
69✔
1644

1645
  STMT_DLOG_E("start to free stmt");
69✔
1646

1647
  if (pStmt->bindThreadInUse) {
69✔
1648
    pStmt->queue.stopQueue = true;
21✔
1649

1650
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
21✔
1651
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
21✔
1652
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
21✔
1653
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
21✔
1654

1655
    (void)taosThreadJoin(pStmt->bindThread, NULL);
21✔
1656
    pStmt->bindThreadInUse = false;
21✔
1657

1658
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
21✔
1659
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
21✔
1660
  }
1661

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

1673
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
69!
1674
  taosMemoryFree(stmt);
69!
1675

1676
  return TSDB_CODE_SUCCESS;
69✔
1677
}
1678

1679
const char* stmtErrstr(TAOS_STMT* stmt) {
18✔
1680
  STscStmt* pStmt = (STscStmt*)stmt;
18✔
1681

1682
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
18!
1683
    return (char*)tstrerror(terrno);
1✔
1684
  }
1685

1686
  pStmt->exec.pRequest->code = terrno;
17✔
1687

1688
  return taos_errstr(pStmt->exec.pRequest);
17✔
1689
}
1690

1691
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
8✔
1692

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

1695
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
80,537✔
1696
  STscStmt* pStmt = (STscStmt*)stmt;
80,537✔
1697

1698
  STMT_DLOG_E("start is insert");
80,537✔
1699

1700
  if (pStmt->sql.type) {
80,578✔
1701
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
60,572!
1702
  } else {
1703
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
20,006✔
1704
  }
1705

1706
  return TSDB_CODE_SUCCESS;
80,595✔
1707
}
1708

1709
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
4✔
1710
  int32_t   code = 0;
4✔
1711
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1712
  int32_t   preCode = pStmt->errCode;
4✔
1713

1714
  STMT_DLOG_E("start to get tag fields");
4!
1715

1716
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1717
    return pStmt->errCode;
×
1718
  }
1719

1720
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1721
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1722
  }
1723

1724
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1725

1726
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1727
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1728
    pStmt->bInfo.needParse = false;
×
1729
  }
1730

1731
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1732
    taos_free_result(pStmt->exec.pRequest);
×
1733
    pStmt->exec.pRequest = NULL;
×
1734
  }
1735

1736
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1737

1738
  if (pStmt->bInfo.needParse) {
4✔
1739
    STMT_ERRI_JRET(stmtParseSql(pStmt));
3✔
1740
  }
1741

1742
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
2✔
1743

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

1750
  if (code != TSDB_CODE_SUCCESS) {
4✔
1751
    taos_free_result(pStmt->exec.pRequest);
3✔
1752
    pStmt->exec.pRequest = NULL;
3✔
1753
  }
1754
  pStmt->errCode = preCode;
4✔
1755

1756
  return code;
4✔
1757
}
1758

1759
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
152✔
1760
  int32_t   code = 0;
152✔
1761
  STscStmt* pStmt = (STscStmt*)stmt;
152✔
1762
  int32_t   preCode = pStmt->errCode;
152✔
1763

1764
  STMT_DLOG_E("start to get col fields");
152!
1765

1766
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
152!
1767
    return pStmt->errCode;
×
1768
  }
1769

1770
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
152!
1771
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1772
  }
1773

1774
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
152!
1775

1776
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
152!
1777
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1778
    pStmt->bInfo.needParse = false;
×
1779
  }
1780

1781
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
152!
1782
    taos_free_result(pStmt->exec.pRequest);
×
1783
    pStmt->exec.pRequest = NULL;
×
1784
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1785
  }
1786

1787
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
152!
1788

1789
  if (pStmt->bInfo.needParse) {
152✔
1790
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1791
  }
1792

1793
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
152!
1794

1795
_return:
152✔
1796

1797
  if (code != TSDB_CODE_SUCCESS) {
152!
1798
    taos_free_result(pStmt->exec.pRequest);
×
1799
    pStmt->exec.pRequest = NULL;
×
1800
  }
1801

1802
  pStmt->errCode = preCode;
152✔
1803

1804
  return code;
152✔
1805
}
1806

1807
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
2✔
1808
  int       code = 0;
2✔
1809
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1810
  int32_t   preCode = pStmt->errCode;
2✔
1811

1812
  STMT_DLOG_E("start to get param num");
2!
1813

1814
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2!
1815
    return pStmt->errCode;
×
1816
  }
1817

1818
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2!
1819

1820
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2!
1821
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1822
    pStmt->bInfo.needParse = false;
×
1823
  }
1824

1825
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2!
1826
    taos_free_result(pStmt->exec.pRequest);
×
1827
    pStmt->exec.pRequest = NULL;
×
1828
  }
1829

1830
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2!
1831

1832
  if (pStmt->bInfo.needParse) {
2✔
1833
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1834
  }
1835

1836
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1837
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1838
  } else {
1839
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1840
  }
1841

1842
_return:
1✔
1843

1844
  if (code != TSDB_CODE_SUCCESS) {
2✔
1845
    taos_free_result(pStmt->exec.pRequest);
1✔
1846
    pStmt->exec.pRequest = NULL;
1✔
1847
  }
1848

1849
  pStmt->errCode = preCode;
2✔
1850

1851
  return code;
2✔
1852
}
1853

1854
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
906✔
1855
  int       code = 0;
906✔
1856
  STscStmt* pStmt = (STscStmt*)stmt;
906✔
1857
  int32_t   preCode = pStmt->errCode;
906✔
1858

1859
  STMT_DLOG_E("start to get param");
906!
1860

1861
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
906!
1862
    return pStmt->errCode;
×
1863
  }
1864

1865
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
906!
1866
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1867
  }
1868

1869
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
906!
1870

1871
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
906!
1872
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1873
    pStmt->bInfo.needParse = false;
×
1874
  }
1875

1876
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
906!
1877
    taos_free_result(pStmt->exec.pRequest);
×
1878
    pStmt->exec.pRequest = NULL;
×
1879
  }
1880

1881
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
906!
1882

1883
  if (pStmt->bInfo.needParse) {
906!
1884
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1885
  }
1886

1887
  int32_t       nums = 0;
906✔
1888
  TAOS_FIELD_E* pField = NULL;
906✔
1889
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
906!
1890
  if (idx >= nums) {
906!
1891
    tscError("idx %d is too big", idx);
×
1892
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1893
  }
1894

1895
  *type = pField[idx].type;
906✔
1896
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
906✔
1897

1898
_return:
906✔
1899

1900
  if (code != TSDB_CODE_SUCCESS) {
906!
1901
    taos_free_result(pStmt->exec.pRequest);
×
1902
    pStmt->exec.pRequest = NULL;
×
1903
  }
1904

1905
  taosMemoryFree(pField);
906!
1906
  pStmt->errCode = preCode;
906✔
1907

1908
  return code;
906✔
1909
}
1910

1911
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
17✔
1912
  STscStmt* pStmt = (STscStmt*)stmt;
17✔
1913

1914
  STMT_DLOG_E("start to use result");
17✔
1915

1916
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
17!
1917
    tscError("useResult only for query statement");
×
1918
    return NULL;
×
1919
  }
1920

1921
  return pStmt->exec.pRequest;
17✔
1922
}
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