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

taosdata / TDengine / #3824

01 Apr 2025 12:26PM UTC coverage: 34.064% (-0.001%) from 34.065%
#3824

push

travis-ci

happyguoxy
test:alter gcda dir

148483 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222466 of 489445 relevant lines covered (45.45%)

762427.9 hits per line

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

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

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

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

65
  return true;
36,013✔
66
}
67

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

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

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

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

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

95
  return code;
974,852✔
96
}
97

98
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
1,904,493✔
99
  int32_t code = 0;
1,904,493✔
100

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
1,904,493!
102
    STMT_LOG_SEQ(newStatus);
1,940,937!
103
  }
104

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

110
  switch (newStatus) {
1,976,708!
111
    case STMT_PREPARE:
106✔
112
      pStmt->errCode = 0;
106✔
113
      break;
106✔
114
    case STMT_SETTBNAME:
34,077✔
115
      if (STMT_STATUS_EQ(INIT)) {
34,077!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
34,077!
119
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
120
      }
121
      break;
34,077✔
122
    case STMT_SETTAGS:
89✔
123
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
89!
124
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
125
      }
126
      break;
89✔
127
    case STMT_FETCH_FIELDS:
121✔
128
      if (STMT_STATUS_EQ(INIT)) {
121!
129
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
130
      }
131
      break;
121✔
132
    case STMT_BIND:
978,412✔
133
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
978,412!
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;
978,412✔
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:
961,773✔
148
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
961,773!
149
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
      }
151
      break;
961,773✔
152
    case STMT_EXECUTE:
2,130✔
153
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,130!
154
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
×
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)) {
2,130!
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      }
163
      break;
2,130✔
164
    default:
×
165
      code = TSDB_CODE_APP_ERROR;
×
166
      break;
×
167
  }
168

169
  STMT_ERR_RET(code);
1,976,708!
170

171
  pStmt->sql.status = newStatus;
1,976,708✔
172

173
  return TSDB_CODE_SUCCESS;
1,976,708✔
174
}
175

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

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

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

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

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

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

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

262
  return TSDB_CODE_SUCCESS;
102✔
263
}
264

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

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

271
  return TSDB_CODE_SUCCESS;
101✔
272
}
273

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

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

281
  pStmt->sql.autoCreateTbl = autoCreateTbl;
101✔
282
  if (pStmt->sql.autoCreateTbl) {
101✔
283
    pStmt->sql.stbInterlaceMode = false;
14✔
284
  }
285

286
  return TSDB_CODE_SUCCESS;
101✔
287
}
288

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

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

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

298
  return TSDB_CODE_SUCCESS;
×
299
}
300

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

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

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

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

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

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

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

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

336
  return TSDB_CODE_SUCCESS;
13✔
337
}
338

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

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

349
  STMT_ERR_RET(stmtCreateRequest(pStmt));
105!
350

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

355
  pStmt->bInfo.needParse = false;
101✔
356

357
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
101!
358
    pStmt->sql.type = STMT_TYPE_INSERT;
73✔
359
    pStmt->sql.stbInterlaceMode = false;
73✔
360
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
28!
361
    pStmt->sql.type = STMT_TYPE_QUERY;
×
362
    pStmt->sql.stbInterlaceMode = false;
×
363

364
    return TSDB_CODE_SUCCESS;
×
365
  }
366

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

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

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

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

387
  if (NULL == pStmt->sql.pBindInfo) {
101!
388
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
102!
389
    if (NULL == pStmt->sql.pBindInfo) {
103!
390
      return terrno;
×
391
    }
392
  }
393

394
  return TSDB_CODE_SUCCESS;
102✔
395
}
396

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

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

413
  return TSDB_CODE_SUCCESS;
2,147✔
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) {
1,939✔
422
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
1,939✔
423
  if (NULL == pTblBuf->pCurBuff) {
1,941✔
424
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
1!
425
    return;
×
426
  }
427
  pTblBuf->buffIdx = 1;
1,940✔
428
  pTblBuf->buffOffset = sizeof(*pQueue->head);
1,940✔
429

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

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

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

455
    size_t keyLen = 0;
279✔
456
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
279✔
457
    while (pIter) {
641✔
458
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
362✔
459
      char*          key = taosHashGetKey(pIter, &keyLen);
362✔
460
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
362✔
461

462
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
362✔
463
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
189✔
464
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
378!
465

466
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
189✔
467
        continue;
189✔
468
      }
469

470
      qDestroyStmtDataBlock(pBlocks);
173✔
471
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
173!
472

473
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
173✔
474
    }
475

476
    if (keepTable) {
279✔
477
      return TSDB_CODE_SUCCESS;
189✔
478
    }
479

480
    taosHashCleanup(pStmt->exec.pBlockHash);
90✔
481
    pStmt->exec.pBlockHash = NULL;
90✔
482

483
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
90✔
484
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
90!
485
  }
486

487
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
2,044!
488

489
  return TSDB_CODE_SUCCESS;
2,043✔
490
}
491

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

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

502
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
104✔
503
  STMT_DLOG_E("start to free SQL info");
104!
504

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

514
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
104✔
515
  while (pIter) {
117✔
516
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
13✔
517

518
    qDestroyStmtDataBlock(pCache->pDataCtx);
13✔
519
    qDestroyBoundColInfo(pCache->boundTags);
13✔
520
    taosMemoryFreeClear(pCache->boundTags);
13!
521

522
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
13✔
523
  }
524
  taosHashCleanup(pStmt->sql.pTableCache);
104✔
525
  pStmt->sql.pTableCache = NULL;
104✔
526

527
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
104!
528
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
104!
529

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

538
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
104✔
539
  pStmt->sql.siInfo.tableColsReady = true;
104✔
540

541
  STMT_DLOG_E("end to free SQL info");
104!
542

543
  return TSDB_CODE_SUCCESS;
104✔
544
}
545

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

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

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

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

568
  *vgId = vgInfo.vgId;
78✔
569

570
  return TSDB_CODE_SUCCESS;
78✔
571
}
572

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

578
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
85!
579

580
  return TSDB_CODE_SUCCESS;
85✔
581
}
582

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

590
  pStmt->bInfo.needParse = true;
113✔
591
  pStmt->bInfo.inExecCache = false;
113✔
592

593
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
113✔
594
  if (pCxtInExec) {
112!
595
    pStmt->bInfo.needParse = false;
×
596
    pStmt->bInfo.inExecCache = true;
×
597

598
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
599

600
    if (pStmt->sql.autoCreateTbl) {
×
601
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
602
      return TSDB_CODE_SUCCESS;
×
603
    }
604
  }
605

606
  if (NULL == pStmt->pCatalog) {
112✔
607
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
27!
608
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
28✔
609
  }
610

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

618
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
28!
619
    return TSDB_CODE_SUCCESS;
28✔
620
  }
621

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

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

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

636
      pStmt->exec.pCurrBlock = pNewBlock;
77✔
637

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

640
      return TSDB_CODE_SUCCESS;
77✔
641
    }
642

643
    STMT_RET(stmtCleanBindInfo(pStmt));
×
644
  }
645

646
  uint64_t uid, suid;
647
  int32_t  vgId;
648
  int8_t   tableType;
649

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

657
  pStmt->stat.ctgGetTbMetaNum++;
8✔
658

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

663
    STMT_ERR_RET(code);
×
664
  }
665

666
  STMT_ERR_RET(code);
8!
667

668
  uid = pTableMeta->uid;
8✔
669
  suid = pTableMeta->suid;
8✔
670
  tableType = pTableMeta->tableType;
8✔
671
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
8✔
672
  vgId = pTableMeta->vgId;
8✔
673

674
  taosMemoryFree(pTableMeta);
8!
675

676
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
8!
677

678
  if (uid == pStmt->bInfo.tbUid) {
8!
679
    pStmt->bInfo.needParse = false;
×
680

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

683
    return TSDB_CODE_SUCCESS;
×
684
  }
685

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

692
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
693
    }
694

695
    pStmt->bInfo.needParse = false;
×
696

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

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

705
    return TSDB_CODE_SUCCESS;
×
706
  }
707

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

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

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

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

726
    pStmt->exec.pCurrBlock = pNewBlock;
8✔
727

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

730
    return TSDB_CODE_SUCCESS;
8✔
731
  }
732

733
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
734

735
  return TSDB_CODE_SUCCESS;
×
736
}
737

738
int32_t stmtResetStmt(STscStmt* pStmt) {
1✔
739
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
1!
740

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

746
  pStmt->sql.status = STMT_INIT;
1✔
747

748
  return TSDB_CODE_SUCCESS;
1✔
749
}
750

751
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
36,012✔
752
  SStmtQNode* pParam = (SStmtQNode*)param;
36,012✔
753

754
  if (pParam->restoreTbCols) {
36,012✔
755
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
36,000✔
756
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
34,059✔
757
      *p = taosArrayInit(20, POINTER_BYTES);
34,059✔
758
      if (*p == NULL) {
34,060!
759
        STMT_ERR_RET(terrno);
×
760
      }
761
    }
762

763
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
1,941✔
764
  } else {
765
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
34,071!
766
                                        &pStmt->sql.siInfo, NULL));
767

768
    // taosMemoryFree(pParam->pTbData);
769

770
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
34,052✔
771
  }
772
  return TSDB_CODE_SUCCESS;
36,009✔
773
}
774

775
void* stmtBindThreadFunc(void* param) {
14✔
776
  setThreadName("stmtBind");
14✔
777

778
  qInfo("stmt bind thread started");
14!
779

780
  STscStmt* pStmt = (STscStmt*)param;
14✔
781

782
  while (true) {
36,022✔
783
    if (pStmt->queue.stopQueue) {
36,036✔
784
      break;
14✔
785
    }
786

787
    SStmtQNode* asyncParam = NULL;
36,022✔
788
    if (!stmtDequeue(pStmt, &asyncParam)) {
36,022✔
789
      continue;
14✔
790
    }
791

792
    int ret = stmtAsyncOutput(pStmt, asyncParam);
36,010✔
793
    if (ret != 0) {
36,009!
794
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
795
    }
796
  }
797

798
  qInfo("stmt bind thread stopped");
14!
799

800
  return NULL;
14✔
801
}
802

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

812
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
14!
813
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
814
    STMT_ERR_RET(terrno);
×
815
  }
816

817
  pStmt->bindThreadInUse = true;
14✔
818

819
  (void)taosThreadAttrDestroy(&thAttr);
14✔
820
  return TSDB_CODE_SUCCESS;
14✔
821
}
822

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

829
  return TSDB_CODE_SUCCESS;
14✔
830
}
831

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

844
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
28!
845
    return terrno;
×
846
  }
847

848
  pTblBuf->pCurBuff = buff;
14✔
849
  pTblBuf->buffIdx = 1;
14✔
850
  pTblBuf->buffOffset = 0;
14✔
851

852
  return TSDB_CODE_SUCCESS;
14✔
853
}
854

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

860
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
103!
861
  if (NULL == pStmt) {
104!
862
    return NULL;
×
863
  }
864

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

871
  pStmt->taos = pObj;
104✔
872
  pStmt->bInfo.needParse = true;
104✔
873
  pStmt->sql.status = STMT_INIT;
104✔
874
  pStmt->reqid = reqid;
104✔
875
  pStmt->errCode = TSDB_CODE_SUCCESS;
104✔
876

877
  if (NULL != pOptions) {
104✔
878
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
14✔
879
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
14!
880
      pStmt->stbInterlaceMode = true;
14✔
881
    }
882
  }
883

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

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

914
  pStmt->sql.siInfo.tableColsReady = true;
104✔
915

916
  STMT_LOG_SEQ(STMT_INIT);
104!
917

918
  tscDebug("stmt:%p initialized", pStmt);
104!
919

920
  return pStmt;
104✔
921
}
922

923
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
106✔
924
  STscStmt* pStmt = (STscStmt*)stmt;
106✔
925

926
  STMT_DLOG_E("start to prepare");
106!
927

928
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
105!
929
    return pStmt->errCode;
×
930
  }
931

932
  if (pStmt->sql.status >= STMT_PREPARE) {
105✔
933
    STMT_ERR_RET(stmtResetStmt(pStmt));
1!
934
  }
935

936
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
105!
937

938
  if (length <= 0) {
106✔
939
    length = strlen(sql);
85✔
940
  }
941

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

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

955
  return TSDB_CODE_SUCCESS;
106✔
956
}
957

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

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

968
  SArray* pTblCols = NULL;
13✔
969
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
13,982✔
970
    pTblCols = taosArrayInit(20, POINTER_BYTES);
13,969✔
971
    if (NULL == pTblCols) {
13,971!
972
      return terrno;
×
973
    }
974

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

980
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
13✔
981

982
  return TSDB_CODE_SUCCESS;
13✔
983
}
984

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

988
  STMT_DLOG("start to set dbName:%s", dbName);
3!
989

990
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3!
991

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

1001
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
34,118✔
1002
  STscStmt* pStmt = (STscStmt*)stmt;
34,118✔
1003

1004
  int64_t startUs = taosGetTimestampUs();
34,134✔
1005

1006
  STMT_DLOG("start to set tbName:%s", tbName);
34,134!
1007

1008
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
34,125!
1009
    return pStmt->errCode;
×
1010
  }
1011

1012
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
34,125!
1013

1014
  int32_t insert = 0;
34,117✔
1015
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
34,117!
1016
  if (0 == insert) {
34,110!
1017
    tscError("set tb name not available for none insert statement");
×
1018
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1019
  }
1020

1021
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
34,110✔
1022
    STMT_ERR_RET(stmtCreateRequest(pStmt));
109!
1023

1024
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
113!
1025
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1026
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
113!
1027

1028
    STMT_ERR_RET(stmtGetFromCache(pStmt));
113!
1029

1030
    if (pStmt->bInfo.needParse) {
113✔
1031
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
28✔
1032
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
28✔
1033

1034
      STMT_ERR_RET(stmtParseSql(pStmt));
28!
1035
    }
1036
  } else {
1037
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
34,001✔
1038
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
34,001✔
1039
    pStmt->exec.pRequest->requestId++;
34,001✔
1040
    pStmt->bInfo.needParse = false;
34,001✔
1041
  }
1042

1043
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
34,114✔
1044
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
14!
1045
  }
1046

1047
  int64_t startUs2 = taosGetTimestampUs();
34,137✔
1048
  pStmt->stat.setTbNameUs += startUs2 - startUs;
34,137✔
1049

1050
  return TSDB_CODE_SUCCESS;
34,137✔
1051
}
1052

1053
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
89✔
1054
  STscStmt* pStmt = (STscStmt*)stmt;
89✔
1055

1056
  STMT_DLOG_E("start to set tbTags");
89!
1057

1058
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
89!
1059
    return pStmt->errCode;
×
1060
  }
1061

1062
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
89!
1063

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

1070
  if (pStmt->bInfo.inExecCache) {
89!
1071
    return TSDB_CODE_SUCCESS;
×
1072
  }
1073

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

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

1086
  return TSDB_CODE_SUCCESS;
89✔
1087
}
1088

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

1094
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
11!
1095
    tscError("invalid operation to get query tag fileds");
×
1096
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1097
  }
1098

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

1106
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
11!
1107

1108
  return TSDB_CODE_SUCCESS;
11✔
1109
}
1110

1111
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
106✔
1112
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
106!
1113
    return pStmt->errCode;
×
1114
  }
1115

1116
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
106!
1117
    tscError("invalid operation to get query column fileds");
×
1118
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1119
  }
1120

1121
  STableDataCxt** pDataBlock = NULL;
106✔
1122

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

1134
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
106!
1135

1136
  return TSDB_CODE_SUCCESS;
106✔
1137
}
1138

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

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

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

1157
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
34,051✔
1158
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
34,051✔
1159
    pStmt->sql.siInfo.pVgroupHash =
1,941✔
1160
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,940✔
1161
  }
1162
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
34,052✔
1163
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
1,940✔
1164
  }
1165

1166
  if (NULL == pStmt->sql.siInfo.pRequest) {
34,053✔
1167
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
14!
1168
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1169

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

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

1179
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
34,053✔
1180
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
122✔
1181
    pStmt->sql.siInfo.tbFromHash = true;
12✔
1182
  }
1183

1184
  if (0 == pStmt->sql.siInfo.firstName[0]) {
34,053✔
1185
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
14✔
1186
  }
1187

1188
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
34,053✔
1189
  param->next = NULL;
34,053✔
1190

1191
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
34,053✔
1192

1193
  stmtEnqueue(pStmt, param);
34,078✔
1194

1195
  return TSDB_CODE_SUCCESS;
34,064✔
1196
}
1197

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

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

1218
  return TSDB_CODE_SUCCESS;
34,044✔
1219
}
1220

1221
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
973,653✔
1222
  STscStmt* pStmt = (STscStmt*)stmt;
973,653✔
1223
  int32_t   code = 0;
973,653✔
1224

1225
  int64_t startUs = taosGetTimestampUs();
970,760✔
1226

1227
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
970,760!
1228

1229
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
965,570!
1230
    return pStmt->errCode;
×
1231
  }
1232

1233
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
965,570!
1234

1235
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
975,274!
1236
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1237
    pStmt->bInfo.needParse = false;
×
1238
  }
1239

1240
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
975,274!
1241
    taos_free_result(pStmt->exec.pRequest);
×
1242
    pStmt->exec.pRequest = NULL;
×
1243
  }
1244

1245
  STMT_ERR_RET(stmtCreateRequest(pStmt));
975,274!
1246

1247
  if (pStmt->bInfo.needParse) {
973,277✔
1248
    STMT_ERR_RET(stmtParseSql(pStmt));
73!
1249
  }
1250

1251
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
974,783!
1252
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
×
1253

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

1267
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
×
1268
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
×
1269

1270
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
×
1271

1272
    if (pStmt->sql.pQuery->haveResultSet) {
×
1273
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
×
1274
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1275
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
×
1276
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
×
1277
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
×
1278
    }
1279

1280
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
×
1281
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
×
1282
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
×
1283

1284
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1285
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1286
    // }
1287

1288
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1289

1290
    return TSDB_CODE_SUCCESS;
×
1291
  }
1292

1293
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
974,783!
1294
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1295
  }
1296

1297
  STableDataCxt** pDataBlock = NULL;
973,046✔
1298

1299
  if (pStmt->exec.pCurrBlock) {
973,046✔
1300
    pDataBlock = &pStmt->exec.pCurrBlock;
972,946✔
1301
  } else {
1302
    pDataBlock =
1303
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
100✔
1304
    if (NULL == pDataBlock) {
100!
1305
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1306
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1307
    }
1308
    pStmt->exec.pCurrBlock = *pDataBlock;
100✔
1309
    if (pStmt->sql.stbInterlaceMode) {
100✔
1310
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
14✔
1311
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
14✔
1312
    }
1313
  }
1314

1315
  int64_t startUs2 = taosGetTimestampUs();
976,862✔
1316
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
976,862✔
1317

1318
  SStmtQNode* param = NULL;
976,862✔
1319
  if (pStmt->sql.stbInterlaceMode) {
976,862✔
1320
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
68,115!
1321
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
68,102!
1322
    taosArrayClear(param->tblData.aCol);
34,044✔
1323

1324
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1325

1326
    param->restoreTbCols = false;
34,015✔
1327
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
34,015✔
1328
  }
1329

1330
  int64_t startUs3 = taosGetTimestampUs();
1,000,002✔
1331
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
1,000,002✔
1332

1333
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
1,000,002✔
1334

1335
  if (colIdx < 0) {
1,000,002!
1336
    if (pStmt->sql.stbInterlaceMode) {
1,008,094✔
1337
      (*pDataBlock)->pData->flags = 0;
34,043✔
1338
      code =
1339
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
34,043✔
1340
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
34,043✔
1341
    } else {
1342
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
974,051✔
1343
                                pStmt->taos->optionInfo.charsetCxt);
974,051✔
1344
    }
1345

1346
    if (code) {
1,007,421✔
1347
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1348
      STMT_ERR_RET(code);
1!
1349
    }
1350
  } else {
1351
    if (pStmt->sql.stbInterlaceMode) {
×
1352
      tscError("bind single column not allowed in stb insert mode");
×
1353
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1354
    }
1355

1356
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1357
      tscError("bind column index not in sequence");
×
1358
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1359
    }
1360

1361
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1362

1363
    if (0 == colIdx) {
×
1364
      pStmt->bInfo.sBindRowNum = bind->num;
30✔
1365
    }
1366

1367
    code =
1368
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
1369
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1370
    if (code) {
140!
1371
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1372
      STMT_ERR_RET(code);
×
1373
    }
1374
  }
1375

1376
  int64_t startUs4 = taosGetTimestampUs();
1,003,873✔
1377
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
1,003,873✔
1378

1379
  if (pStmt->sql.stbInterlaceMode) {
1,003,873✔
1380
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
34,054!
1381
  }
1382

1383
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
1,005,828✔
1384

1385
  return TSDB_CODE_SUCCESS;
1,005,828✔
1386
}
1387

1388
int stmtAddBatch(TAOS_STMT* stmt) {
953,049✔
1389
  STscStmt* pStmt = (STscStmt*)stmt;
953,049✔
1390

1391
  int64_t startUs = taosGetTimestampUs();
951,497✔
1392

1393
  STMT_DLOG_E("start to add batch");
951,497!
1394

1395
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
955,834!
1396
    return pStmt->errCode;
×
1397
  }
1398

1399
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
955,834!
1400

1401
  if (pStmt->sql.stbInterlaceMode) {
957,300✔
1402
    int64_t startUs2 = taosGetTimestampUs();
1,941✔
1403
    pStmt->stat.addBatchUs += startUs2 - startUs;
1,941✔
1404

1405
    pStmt->sql.siInfo.tableColsReady = false;
1,941✔
1406

1407
    SStmtQNode* param = NULL;
1,941✔
1408
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
3,882!
1409
    param->restoreTbCols = true;
1,941✔
1410
    param->next = NULL;
1,941✔
1411

1412
    stmtEnqueue(pStmt, param);
1,941✔
1413

1414
    return TSDB_CODE_SUCCESS;
1,941✔
1415
  }
1416

1417
  STMT_ERR_RET(stmtCacheBlock(pStmt));
955,359✔
1418

1419
  return TSDB_CODE_SUCCESS;
951,936✔
1420
}
1421
/*
1422
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1423
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1424

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

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

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

1447
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1448
        continue;
1449
      }
1450

1451
      break;
1452
    }
1453

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

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

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

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

1485
      pStmt->stat.ctgGetTbMetaNum++;
1486

1487
      taos_free_result(pStmt->exec.pRequest);
1488
      pStmt->exec.pRequest = NULL;
1489

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

1497
      pMeta->uid = pTableMeta->uid;
1498
      pStmt->bInfo.tbUid = pTableMeta->uid;
1499
      taosMemoryFree(pTableMeta);
1500
    }
1501

1502
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1503
  }
1504

1505
  return finalCode;
1506
}
1507
*/
1508

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

1518
  STMT_DLOG_E("start to exec");
1519

1520
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1521

1522
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1523
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1524

1525
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1526

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

1538
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1539

1540
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1541
  pStmt->affectedRows += pStmt->exec.affectedRows;
1542

1543
_return:
1544

1545
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1546

1547
  tFreeSSubmitRsp(pRsp);
1548

1549
  ++pStmt->sql.runTimes;
1550

1551
  STMT_RET(code);
1552
}
1553
*/
1554

1555
int stmtExec(TAOS_STMT* stmt) {
2,130✔
1556
  STscStmt*   pStmt = (STscStmt*)stmt;
2,130✔
1557
  int32_t     code = 0;
2,130✔
1558
  SSubmitRsp* pRsp = NULL;
2,130✔
1559

1560
  int64_t startUs = taosGetTimestampUs();
2,130✔
1561

1562
  STMT_DLOG_E("start to exec");
2,130!
1563

1564
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,130!
1565
    return pStmt->errCode;
×
1566
  }
1567

1568
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2,130!
1569

1570
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,130!
1571
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
×
1572
  } else {
1573
    if (pStmt->sql.stbInterlaceMode) {
2,130✔
1574
      int64_t startTs = taosGetTimestampUs();
1,941✔
1575
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
8,471✔
1576
        taosUsleep(1);
6,530✔
1577
      }
1578
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
1,941✔
1579

1580
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
1,941!
1581
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
1,941✔
1582
      pStmt->sql.siInfo.pVgroupHash = NULL;
1,941✔
1583
      pStmt->sql.siInfo.pVgroupList = NULL;
1,941✔
1584
    } else {
1585
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
189✔
1586
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
189!
1587

1588
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
189!
1589

1590
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
189!
1591
    }
1592

1593
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2,130✔
1594
  }
1595

1596
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2,129!
1597
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1598
    if (code) {
×
1599
      pStmt->exec.pRequest->code = code;
×
1600
    } else {
1601
      tFreeSSubmitRsp(pRsp);
×
1602
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1603
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1604
    }
1605
  }
1606

1607
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2,129!
1608

1609
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2,129✔
1610
  pStmt->affectedRows += pStmt->exec.affectedRows;
2,129✔
1611

1612
_return:
2,129✔
1613

1614
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
2,176✔
1615
    taosUsleep(1);
47✔
1616
  }
1617

1618
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
2,130!
1619

1620
  tFreeSSubmitRsp(pRsp);
2,128✔
1621

1622
  ++pStmt->sql.runTimes;
2,128✔
1623

1624
  int64_t startUs2 = taosGetTimestampUs();
2,130✔
1625
  pStmt->stat.execUseUs += startUs2 - startUs;
2,130✔
1626

1627
  STMT_RET(code);
2,130!
1628
}
1629

1630
int stmtClose(TAOS_STMT* stmt) {
103✔
1631
  STscStmt* pStmt = (STscStmt*)stmt;
103✔
1632

1633
  STMT_DLOG_E("start to free stmt");
103!
1634

1635
  if (pStmt->bindThreadInUse) {
103✔
1636
    pStmt->queue.stopQueue = true;
14✔
1637

1638
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
14✔
1639
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
14✔
1640
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
14✔
1641
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
14✔
1642

1643
    (void)taosThreadJoin(pStmt->bindThread, NULL);
14✔
1644
    pStmt->bindThreadInUse = false;
14✔
1645

1646
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
14✔
1647
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
14✔
1648
  }
1649

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

1661
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
103!
1662
  taosMemoryFree(stmt);
103!
1663

1664
  return TSDB_CODE_SUCCESS;
103✔
1665
}
1666

1667
const char* stmtErrstr(TAOS_STMT* stmt) {
×
1668
  STscStmt* pStmt = (STscStmt*)stmt;
×
1669

1670
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
1671
    return (char*)tstrerror(terrno);
×
1672
  }
1673

1674
  pStmt->exec.pRequest->code = terrno;
×
1675

1676
  return taos_errstr(pStmt->exec.pRequest);
×
1677
}
1678

1679
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
2✔
1680

1681
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
48✔
1682

1683
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
1,001,699✔
1684
  STscStmt* pStmt = (STscStmt*)stmt;
1,001,699✔
1685

1686
  STMT_DLOG_E("start is insert");
1,001,699!
1687

1688
  if (pStmt->sql.type) {
1,010,061✔
1689
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,009,961!
1690
  } else {
1691
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
100✔
1692
  }
1693

1694
  return TSDB_CODE_SUCCESS;
1,010,062✔
1695
}
1696

1697
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
14✔
1698
  int32_t   code = 0;
14✔
1699
  STscStmt* pStmt = (STscStmt*)stmt;
14✔
1700
  int32_t   preCode = pStmt->errCode;
14✔
1701

1702
  STMT_DLOG_E("start to get tag fields");
14!
1703

1704
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
14!
1705
    return pStmt->errCode;
×
1706
  }
1707

1708
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
14!
1709
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1710
  }
1711

1712
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
14!
1713

1714
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
14!
1715
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1716
    pStmt->bInfo.needParse = false;
×
1717
  }
1718

1719
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
14!
1720
    taos_free_result(pStmt->exec.pRequest);
×
1721
    pStmt->exec.pRequest = NULL;
×
1722
  }
1723

1724
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
14!
1725

1726
  if (pStmt->bInfo.needParse) {
14✔
1727
    STMT_ERRI_JRET(stmtParseSql(pStmt));
3✔
1728
  }
1729

1730
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
12✔
1731

1732
_return:
11✔
1733
  // compatible with previous versions
1734
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
14!
1735
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
1736
  }
1737
  pStmt->errCode = preCode;
14✔
1738

1739
  return code;
14✔
1740
}
1741

1742
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
101✔
1743
  int32_t   code = 0;
101✔
1744
  STscStmt* pStmt = (STscStmt*)stmt;
101✔
1745
  int32_t   preCode = pStmt->errCode;
101✔
1746

1747
  STMT_DLOG_E("start to get col fields");
101!
1748

1749
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
101!
1750
    return pStmt->errCode;
×
1751
  }
1752

1753
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
101!
1754
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1755
  }
1756

1757
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
101!
1758

1759
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
101!
1760
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1761
    pStmt->bInfo.needParse = false;
×
1762
  }
1763

1764
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
101!
1765
    taos_free_result(pStmt->exec.pRequest);
×
1766
    pStmt->exec.pRequest = NULL;
×
1767
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1768
  }
1769

1770
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
101!
1771

1772
  if (pStmt->bInfo.needParse) {
101!
1773
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1774
  }
1775

1776
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
101!
1777

1778
_return:
101✔
1779

1780
  pStmt->errCode = preCode;
101✔
1781

1782
  return code;
101✔
1783
}
1784

1785
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
2✔
1786
  int       code = 0;
2✔
1787
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1788
  int32_t   preCode = pStmt->errCode;
2✔
1789

1790
  STMT_DLOG_E("start to get param num");
2!
1791

1792
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2!
1793
    return pStmt->errCode;
×
1794
  }
1795

1796
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2!
1797

1798
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2!
1799
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1800
    pStmt->bInfo.needParse = false;
×
1801
  }
1802

1803
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2!
1804
    taos_free_result(pStmt->exec.pRequest);
×
1805
    pStmt->exec.pRequest = NULL;
×
1806
  }
1807

1808
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2!
1809

1810
  if (pStmt->bInfo.needParse) {
2✔
1811
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1812
  }
1813

1814
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1815
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1816
  } else {
1817
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1818
  }
1819

1820
_return:
1✔
1821

1822
  pStmt->errCode = preCode;
2✔
1823

1824
  return code;
2✔
1825
}
1826

1827
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
4✔
1828
  int       code = 0;
4✔
1829
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1830
  int32_t   preCode = pStmt->errCode;
4✔
1831

1832
  STMT_DLOG_E("start to get param");
4!
1833

1834
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1835
    return pStmt->errCode;
×
1836
  }
1837

1838
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1839
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1840
  }
1841

1842
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1843

1844
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1845
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1846
    pStmt->bInfo.needParse = false;
×
1847
  }
1848

1849
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1850
    taos_free_result(pStmt->exec.pRequest);
×
1851
    pStmt->exec.pRequest = NULL;
×
1852
  }
1853

1854
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1855

1856
  if (pStmt->bInfo.needParse) {
4!
1857
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1858
  }
1859

1860
  int32_t       nums = 0;
4✔
1861
  TAOS_FIELD_E* pField = NULL;
4✔
1862
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
4!
1863
  if (idx >= nums) {
4!
1864
    tscError("idx %d is too big", idx);
×
1865
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1866
  }
1867

1868
  *type = pField[idx].type;
4✔
1869
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
4✔
1870

1871
_return:
4✔
1872

1873
  taosMemoryFree(pField);
4!
1874
  pStmt->errCode = preCode;
4✔
1875

1876
  return code;
4✔
1877
}
1878

1879
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
×
1880
  STscStmt* pStmt = (STscStmt*)stmt;
×
1881

1882
  STMT_DLOG_E("start to use result");
×
1883

1884
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
1885
    tscError("useResult only for query statement");
×
1886
    return NULL;
×
1887
  }
1888

1889
  return pStmt->exec.pRequest;
×
1890
}
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