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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

65.31
/source/client/src/clientStmt.c
1

2
#include "clientInt.h"
3
#include "clientLog.h"
4
#include "tdef.h"
5

6
#include "catalog.h"
7
#include "clientStmt.h"
8
#include "parser.h"
9

10
char* gStmtStatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
11
                          "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
12

13
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
14
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
10,275✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
10,279✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
10,279✔
17
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
18
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
19
    if (NULL == pTblBuf->pCurBuff) {
×
20
      return TAOS_GET_TERRNO(terrno);
×
21
    }
22
    *pBuf = pTblBuf->pCurBuff;
×
23
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
24
  } else {
25
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
26
    if (NULL == buff) {
×
27
      return terrno;
×
28
    }
29

30
    if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
×
31
      return terrno;
×
32
    }
33

34
    pTblBuf->buffIdx++;
×
35
    pTblBuf->pCurBuff = buff;
×
36
    *pBuf = buff;
×
37
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
38
  }
39

40
  return TSDB_CODE_SUCCESS;
10,279✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
10,274✔
44
  int i = 0;
10,274✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
61,763✔
46
    if (i < 10) {
51,521✔
47
      taosUsleep(1);
46,856✔
48
      i++;
46,827✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
4,665✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
4,664✔
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
4,664✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
4,663✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
10,208✔
58
    return false;
95✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
10,113✔
61
  SStmtQNode* node = pStmt->queue.head->next;
10,113✔
62
  pStmt->queue.head = pStmt->queue.head->next;
10,113✔
63
  *param = node;
10,113✔
64

65
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
10,113✔
66

67
  return true;
10,185✔
68
}
69

70
void stmtEnqueue(STscStmt* pStmt, SStmtQNode* param) {
10,183✔
71
  pStmt->queue.tail->next = param;
10,183✔
72
  pStmt->queue.tail = param;
10,183✔
73

74
  pStmt->stat.bindDataNum++;
10,183✔
75

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

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
119,044✔
83
  int32_t code = 0;
119,044✔
84

85
  if (pStmt->exec.pRequest == NULL) {
119,044✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
11,030✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
11,031!
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
11,031!
92
      pStmt->exec.pRequest->syncQuery = true;
11,037✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
11,037✔
94
    }
95
  }
96

97
  return code;
119,045✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
209,168✔
101
  int32_t code = 0;
209,168✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
209,168!
104
    STMT_LOG_SEQ(newStatus);
209,475!
105
  }
106

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

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

171
  STMT_ERR_RET(code);
212,924!
172

173
  pStmt->sql.status = newStatus;
212,924✔
174

175
  return TSDB_CODE_SUCCESS;
212,924✔
176
}
177

178
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
10,083✔
179
  STscStmt* pStmt = (STscStmt*)stmt;
10,083✔
180

181
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
10,083✔
182

183
  if ('\0' == pStmt->bInfo.tbName[0]) {
10,083✔
184
    tscError("no table name set");
4!
185
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
6✔
186
  }
187

188
  *tbName = pStmt->bInfo.tbName;
10,081✔
189

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

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

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

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

213
  return TSDB_CODE_SUCCESS;
214
}
215

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

220
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
221
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
222

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

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

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

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

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

264
  return TSDB_CODE_SUCCESS;
10,927✔
265
}
266

267
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
10,902✔
268
  STscStmt* pStmt = (STscStmt*)stmt;
10,902✔
269

270
  pStmt->sql.pVgHash = pVgHash;
10,902✔
271
  pStmt->exec.pBlockHash = pBlockHash;
10,902✔
272

273
  return TSDB_CODE_SUCCESS;
10,902✔
274
}
275

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

281
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
10,906!
282
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
10,925!
283

284
  pStmt->sql.autoCreateTbl = autoCreateTbl;
10,929✔
285
  if (pStmt->sql.autoCreateTbl) {
10,929✔
286
    pStmt->sql.stbInterlaceMode = false;
10,017✔
287
  }
288

289
  return TSDB_CODE_SUCCESS;
10,929✔
290
}
291

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

295
  *pVgHash = pStmt->sql.pVgHash;
×
296
  pStmt->sql.pVgHash = NULL;
×
297

298
  *pBlockHash = pStmt->exec.pBlockHash;
×
299
  pStmt->exec.pBlockHash = NULL;
×
300

301
  return TSDB_CODE_SUCCESS;
×
302
}
303

304
int32_t stmtCacheBlock(STscStmt* pStmt) {
79,867✔
305
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
79,867✔
306
    return TSDB_CODE_SUCCESS;
69,980✔
307
  }
308

309
  uint64_t uid = pStmt->bInfo.tbUid;
9,887✔
310
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
9,887!
311

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

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

322
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
10,015!
323

324
  SStmtTableCache cache = {
10,004✔
325
      .pDataCtx = pDst,
326
      .boundTags = pStmt->bInfo.boundTags,
10,004✔
327
  };
328

329
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
10,004✔
330
    return terrno;
3✔
331
  }
332

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

339
  return TSDB_CODE_SUCCESS;
10,017✔
340
}
341

342
int32_t stmtParseSql(STscStmt* pStmt) {
10,927✔
343
  pStmt->exec.pCurrBlock = NULL;
10,927✔
344

345
  SStmtCallback stmtCb = {
10,927✔
346
      .pStmt = pStmt,
347
      .getTbNameFn = stmtGetTbName,
348
      .setInfoFn = stmtUpdateInfo,
349
      .getExecInfoFn = stmtGetExecInfo,
350
  };
351

352
  STMT_ERR_RET(stmtCreateRequest(pStmt));
10,927!
353
  pStmt->exec.pRequest->stmtBindVersion = 1;
10,930✔
354

355
  pStmt->stat.parseSqlNum++;
10,930✔
356
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
10,930✔
357
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
10,913✔
358

359
  pStmt->bInfo.needParse = false;
10,913✔
360

361
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
10,913!
362
    pStmt->sql.type = STMT_TYPE_INSERT;
835✔
363
    pStmt->sql.stbInterlaceMode = false;
835✔
364
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
10,078✔
365
    pStmt->sql.type = STMT_TYPE_QUERY;
2✔
366
    pStmt->sql.stbInterlaceMode = false;
2✔
367

368
    return TSDB_CODE_SUCCESS;
2✔
369
  }
370

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

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

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

387
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
909✔
388
    }
389
  }
390

391
  if (NULL == pStmt->sql.pBindInfo) {
10,932✔
392
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
10,931!
393
    if (NULL == pStmt->sql.pBindInfo) {
10,911!
394
      return terrno;
×
395
    }
396
  }
397

398
  return TSDB_CODE_SUCCESS;
10,912✔
399
}
400

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

409
  pStmt->bInfo.tbName[0] = 0;
26,521✔
410
  pStmt->bInfo.tbFName[0] = 0;
26,521✔
411
  if (!pStmt->bInfo.tagsCached) {
26,521✔
412
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
6,501✔
413
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
6,497!
414
  }
415
  pStmt->bInfo.stbFName[0] = 0;
26,518✔
416

417
  return TSDB_CODE_SUCCESS;
26,518✔
418
}
419

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

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

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

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

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

460
    size_t keyLen = 0;
21,790✔
461
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
21,790✔
462
    while (pIter) {
43,559✔
463
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
21,756✔
464
      char*          key = taosHashGetKey(pIter, &keyLen);
21,756✔
465
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
21,752✔
466

467
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
21,757✔
468
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
10,883✔
469
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
21,767!
470

471
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
10,868✔
472
        continue;
10,882✔
473
      }
474

475
      qDestroyStmtDataBlock(pBlocks);
10,874✔
476
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
10,870!
477

478
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
10,868✔
479
    }
480

481
    if (keepTable) {
21,803✔
482
      return TSDB_CODE_SUCCESS;
10,884✔
483
    }
484

485
    taosHashCleanup(pStmt->exec.pBlockHash);
10,919✔
486
    pStmt->exec.pBlockHash = NULL;
10,919✔
487

488
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,919✔
489
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,919!
490
  }
491

492
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
15,540!
493

494
  return TSDB_CODE_SUCCESS;
15,540✔
495
}
496

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

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

507
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
10,982✔
508
  STMT_DLOG_E("start to free SQL info");
10,982!
509

510
  taosMemoryFree(pStmt->sql.pBindInfo);
10,982!
511
  taosMemoryFree(pStmt->sql.queryRes.fields);
10,985!
512
  taosMemoryFree(pStmt->sql.queryRes.userFields);
10,994!
513
  taosMemoryFree(pStmt->sql.sqlStr);
10,987!
514
  qDestroyQuery(pStmt->sql.pQuery);
10,997✔
515
  taosArrayDestroy(pStmt->sql.nodeList);
10,998✔
516
  taosHashCleanup(pStmt->sql.pVgHash);
10,987✔
517
  pStmt->sql.pVgHash = NULL;
10,997✔
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
10,997✔
520
  while (pIter) {
21,004✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
10,011✔
522

523
    qDestroyStmtDataBlock(pCache->pDataCtx);
10,011✔
524
    qDestroyBoundColInfo(pCache->boundTags);
10,016✔
525
    taosMemoryFreeClear(pCache->boundTags);
10,016!
526

527
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
10,012✔
528
  }
529
  taosHashCleanup(pStmt->sql.pTableCache);
10,993✔
530
  pStmt->sql.pTableCache = NULL;
10,998✔
531

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
10,998!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
10,991!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
10,986✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
10,993✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
10,995✔
538
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
10,986✔
539
  tSimpleHashCleanup(pStmt->sql.predicateCols);
10,995✔
540
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
10,996✔
541
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
10,994!
542
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
10,993✔
543
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
10,991✔
544

545
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
10,992✔
546
  pStmt->sql.siInfo.tableColsReady = true;
10,992✔
547

548
  STMT_DLOG_E("end to free SQL info");
10,992!
549

550
  return TSDB_CODE_SUCCESS;
10,981✔
551
}
552

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

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

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

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

575
  *vgId = vgInfo.vgId;
9✔
576

577
  return TSDB_CODE_SUCCESS;
9✔
578
}
579

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

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

587
  return TSDB_CODE_SUCCESS;
16✔
588
}
589

590
int32_t stmtGetTableMeta(STscStmt* pStmt, STableMeta** ppTableMeta) {
12✔
591
  if (NULL == pStmt->pCatalog) {
12✔
592
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
9!
593
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
9✔
594
  }
595

596
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
12✔
597
                           .requestId = pStmt->exec.pRequest->requestId,
12✔
598
                           .requestObjRefId = pStmt->exec.pRequest->self,
12✔
599
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
12✔
600

601
  int32_t code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, ppTableMeta);
12✔
602
  if (TSDB_CODE_SUCCESS != code) {
12✔
603
    STMT_ELOG("get table meta failed, code: 0x%x", code);
2!
604
    return code;
2✔
605
  }
606

607
  return TSDB_CODE_SUCCESS;
10✔
608
}
609

610
int32_t stmtGetFromCache(STscStmt* pStmt) {
10,113✔
611
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
10,113!
612
    pStmt->bInfo.needParse = false;
×
613
    pStmt->bInfo.inExecCache = false;
×
614
    return TSDB_CODE_SUCCESS;
×
615
  }
616

617
  pStmt->bInfo.needParse = true;
10,113✔
618
  pStmt->bInfo.inExecCache = false;
10,113✔
619

620
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,113✔
621
  if (pCxtInExec) {
10,128✔
622
    pStmt->bInfo.needParse = false;
16✔
623
    pStmt->bInfo.inExecCache = true;
16✔
624

625
    pStmt->exec.pCurrBlock = *pCxtInExec;
16✔
626

627
    if (pStmt->sql.autoCreateTbl) {
16!
628
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
16!
629
      return TSDB_CODE_SUCCESS;
16✔
630
    }
631
  }
632

633
  if (NULL == pStmt->pCatalog) {
10,112✔
634
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
102!
635
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
102✔
636
  }
637

638
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
10,112!
639
    if (pStmt->bInfo.inExecCache) {
10,089!
640
      pStmt->bInfo.needParse = false;
×
641
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
642
      return TSDB_CODE_SUCCESS;
×
643
    }
644

645
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
10,089!
646
    return TSDB_CODE_SUCCESS;
10,090✔
647
  }
648

649
  if (pStmt->sql.autoCreateTbl) {
16✔
650
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
8✔
651
    if (pCache) {
8!
652
      pStmt->bInfo.needParse = false;
8✔
653
      pStmt->bInfo.tbUid = 0;
8✔
654

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

658
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
659
                      POINTER_BYTES)) {
660
        STMT_ERR_RET(terrno);
×
661
      }
662

663
      pStmt->exec.pCurrBlock = pNewBlock;
8✔
664

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

667
      return TSDB_CODE_SUCCESS;
8✔
668
    }
669

670
    STMT_RET(stmtCleanBindInfo(pStmt));
×
671
  }
672

673
  uint64_t uid, suid;
674
  int32_t  vgId;
675
  int8_t   tableType;
676

677
  STableMeta*      pTableMeta = NULL;
8✔
678
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
8✔
679
                           .requestId = pStmt->exec.pRequest->requestId,
8✔
680
                           .requestObjRefId = pStmt->exec.pRequest->self,
8✔
681
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
8✔
682
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
8✔
683

684
  pStmt->stat.ctgGetTbMetaNum++;
8✔
685

686
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
8!
687
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
688
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
689

690
    STMT_ERR_RET(code);
×
691
  }
692

693
  STMT_ERR_RET(code);
8!
694

695
  uid = pTableMeta->uid;
8✔
696
  suid = pTableMeta->suid;
8✔
697
  tableType = pTableMeta->tableType;
8✔
698
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
8✔
699
  vgId = pTableMeta->vgId;
8✔
700

701
  taosMemoryFree(pTableMeta);
8!
702

703
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
8!
704

705
  if (uid == pStmt->bInfo.tbUid) {
8!
706
    pStmt->bInfo.needParse = false;
×
707

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

710
    return TSDB_CODE_SUCCESS;
×
711
  }
712

713
  if (pStmt->bInfo.inExecCache) {
8!
714
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
715
    if (NULL == pCache) {
×
716
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
717
               pStmt->bInfo.tbFName, uid, cacheUid);
718

719
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
720
    }
721

722
    pStmt->bInfo.needParse = false;
×
723

724
    pStmt->bInfo.tbUid = uid;
×
725
    pStmt->bInfo.tbSuid = suid;
×
726
    pStmt->bInfo.tbType = tableType;
×
727
    pStmt->bInfo.boundTags = pCache->boundTags;
×
728
    pStmt->bInfo.tagsCached = true;
×
729

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

732
    return TSDB_CODE_SUCCESS;
×
733
  }
734

735
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
8✔
736
  if (pCache) {
8!
737
    pStmt->bInfo.needParse = false;
8✔
738

739
    pStmt->bInfo.tbUid = uid;
8✔
740
    pStmt->bInfo.tbSuid = suid;
8✔
741
    pStmt->bInfo.tbType = tableType;
8✔
742
    pStmt->bInfo.boundTags = pCache->boundTags;
8✔
743
    pStmt->bInfo.tagsCached = true;
8✔
744

745
    STableDataCxt* pNewBlock = NULL;
8✔
746
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
8!
747

748
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
749
                    POINTER_BYTES)) {
750
      STMT_ERR_RET(terrno);
×
751
    }
752

753
    pStmt->exec.pCurrBlock = pNewBlock;
8✔
754

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

757
    return TSDB_CODE_SUCCESS;
8✔
758
  }
759

760
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
761

762
  return TSDB_CODE_SUCCESS;
×
763
}
764

765
int32_t stmtResetStmt(STscStmt* pStmt) {
9,989✔
766
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
9,989!
767

768
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,986✔
769
  if (NULL == pStmt->sql.pTableCache) {
9,991!
770
    STMT_ERR_RET(terrno);
×
771
  }
772

773
  if (pStmt->sql.siInfo.pTableRowDataHash) {
9,991!
774
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
775
  }
776

777
  pStmt->sql.status = STMT_INIT;
9,995✔
778

779
  return TSDB_CODE_SUCCESS;
9,995✔
780
}
781

782
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
10,182✔
783
  SStmtQNode* pParam = (SStmtQNode*)param;
10,182✔
784

785
  if (pParam->restoreTbCols) {
10,182✔
786
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
10,183✔
787
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
5,629✔
788
      *p = taosArrayInit(20, POINTER_BYTES);
5,629✔
789
      if (*p == NULL) {
5,630!
790
        STMT_ERR_RET(terrno);
×
791
      }
792
    }
793

794
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
4,554✔
795
  } else {
796
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
5,627!
797
                                        &pStmt->sql.siInfo));
798

799
    // taosMemoryFree(pParam->pTbData);
800

801
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,629✔
802
  }
803
  return TSDB_CODE_SUCCESS;
10,182✔
804
}
805

806
void* stmtBindThreadFunc(void* param) {
96✔
807
  setThreadName("stmtBind");
96✔
808

809
  qInfo("stmt bind thread started");
96!
810

811
  STscStmt* pStmt = (STscStmt*)param;
96✔
812

813
  while (true) {
10,277✔
814
    if (pStmt->queue.stopQueue) {
10,373✔
815
      break;
95✔
816
    }
817

818
    SStmtQNode* asyncParam = NULL;
10,278✔
819
    if (!stmtDequeue(pStmt, &asyncParam)) {
10,278✔
820
      continue;
95✔
821
    }
822

823
    int ret = stmtAsyncOutput(pStmt, asyncParam);
10,183✔
824
    if (ret != 0) {
10,182!
825
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
826
    }
827
  }
828

829
  qInfo("stmt bind thread stopped");
95!
830

831
  return NULL;
95✔
832
}
833

834
int32_t stmtStartBindThread(STscStmt* pStmt) {
95✔
835
  TdThreadAttr thAttr;
836
  if (taosThreadAttrInit(&thAttr) != 0) {
95!
837
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
838
  }
839
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
96!
840
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
841
  }
842

843
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
96!
844
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
845
    STMT_ERR_RET(terrno);
×
846
  }
847

848
  pStmt->bindThreadInUse = true;
96✔
849

850
  (void)taosThreadAttrDestroy(&thAttr);
96✔
851
  return TSDB_CODE_SUCCESS;
96✔
852
}
853

854
int32_t stmtInitQueue(STscStmt* pStmt) {
96✔
855
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
96✔
856
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
96✔
857
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
192!
858
  pStmt->queue.tail = pStmt->queue.head;
96✔
859

860
  return TSDB_CODE_SUCCESS;
96✔
861
}
862

863
int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
96✔
864
  pTblBuf->buffUnit = sizeof(SStmtQNode);
96✔
865
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
96✔
866
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
96✔
867
  if (NULL == pTblBuf->pBufList) {
96!
868
    return terrno;
×
869
  }
870
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
96!
871
  if (NULL == buff) {
96!
872
    return terrno;
×
873
  }
874

875
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
192!
876
    return terrno;
×
877
  }
878

879
  pTblBuf->pCurBuff = buff;
96✔
880
  pTblBuf->buffIdx = 1;
96✔
881
  pTblBuf->buffOffset = 0;
96✔
882

883
  return TSDB_CODE_SUCCESS;
96✔
884
}
885

886
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid, TAOS_STMT_OPTIONS* pOptions) {
1,005✔
887
  STscObj*  pObj = (STscObj*)taos;
1,005✔
888
  STscStmt* pStmt = NULL;
1,005✔
889
  int32_t   code = 0;
1,005✔
890

891
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
1,005!
892
  if (NULL == pStmt) {
1,007!
893
    return NULL;
×
894
  }
895

896
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1,007✔
897
  if (NULL == pStmt->sql.pTableCache) {
1,006!
898
    taosMemoryFree(pStmt);
×
899
    return NULL;
×
900
  }
901

902
  pStmt->taos = pObj;
1,006✔
903
  pStmt->bInfo.needParse = true;
1,006✔
904
  pStmt->sql.status = STMT_INIT;
1,006✔
905
  pStmt->reqid = reqid;
1,006✔
906
  pStmt->errCode = TSDB_CODE_SUCCESS;
1,006✔
907

908
  if (NULL != pOptions) {
1,006✔
909
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
96✔
910
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
96!
911
      pStmt->stbInterlaceMode = true;
96✔
912
    }
913
  }
914

915
  if (pStmt->stbInterlaceMode) {
1,006✔
916
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
96✔
917
    pStmt->sql.siInfo.acctId = taos->acctId;
96✔
918
    pStmt->sql.siInfo.dbname = taos->db;
96✔
919
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
96✔
920
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
96✔
921
    if (NULL == pStmt->sql.siInfo.pTableHash) {
96!
922
      (void)stmtClose(pStmt);
×
923
      return NULL;
×
924
    }
925
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
96✔
926
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
96!
927
      STMT_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
928
      (void)stmtClose(pStmt);
×
929
      return NULL;
×
930
    }
931
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
96✔
932
    if (NULL == pStmt->sql.siInfo.pTableCols) {
96!
933
      (void)stmtClose(pStmt);
×
934
      return NULL;
×
935
    }
936

937
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
96✔
938
    if (TSDB_CODE_SUCCESS == code) {
96!
939
      code = stmtInitQueue(pStmt);
96✔
940
    }
941
    if (TSDB_CODE_SUCCESS == code) {
96!
942
      code = stmtStartBindThread(pStmt);
96✔
943
    }
944
    if (TSDB_CODE_SUCCESS != code) {
96!
945
      terrno = code;
×
946
      (void)stmtClose(pStmt);
×
947
      return NULL;
×
948
    }
949
  }
950

951
  pStmt->sql.siInfo.tableColsReady = true;
1,006✔
952

953
  STMT_LOG_SEQ(STMT_INIT);
1,006!
954

955
  tscDebug("stmt:%p initialized", pStmt);
1,006!
956

957
  return pStmt;
1,006✔
958
}
959

960
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
10,997✔
961
  STscStmt* pStmt = (STscStmt*)stmt;
10,997✔
962

963
  STMT_DLOG_E("start to prepare");
10,997!
964

965
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11,006!
966
    return pStmt->errCode;
×
967
  }
968

969
  if (pStmt->sql.status >= STMT_PREPARE) {
11,006✔
970
    STMT_ERR_RET(stmtResetStmt(pStmt));
10,000!
971
  }
972

973
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
10,996!
974

975
  if (length <= 0) {
10,998✔
976
    length = strlen(sql);
917✔
977
  }
978

979
  pStmt->sql.sqlStr = taosStrndup(sql, length);
10,998!
980
  if (!pStmt->sql.sqlStr) {
11,007!
981
    return terrno;
×
982
  }
983
  pStmt->sql.sqlLen = length;
11,007✔
984
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
11,007✔
985

986
  STMT_ERR_RET(stmtCreateRequest(pStmt));
11,007!
987

988
  int32_t code = 0;
11,000✔
989
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
10,996✔
990
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
11,000✔
991
    // get table meta
992
    STableMeta* pTableMeta = NULL;
18✔
993
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
22✔
994

995
    char* newSql = NULL;
10✔
996

997
    // conver update sql to insert sql
998
    pStmt->sql.predicateCols = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
10✔
999
    if (NULL == pStmt->sql.predicateCols) {
10!
1000
      STMT_ELOG("fail to allocate memory for predicateCols:%s", tstrerror(terrno));
×
1001
      return terrno;
×
1002
    }
1003

1004
    code = convertUpdateToInsert(sql, &newSql, pTableMeta, pStmt->sql.predicateCols, pStmt->exec.pRequest->msgBuf,
20✔
1005
                                 pStmt->exec.pRequest->msgBufLen);
10✔
1006
    taosMemoryFree(pTableMeta);
10!
1007

1008
    if (TSDB_CODE_SUCCESS != code) {
10✔
1009
      STMT_ELOG("convert update sql to insert sql failed, code: 0x%x", code);
4!
1010
      return code;
4✔
1011
    }
1012

1013
    // reset request sql
1014
    if (pStmt->exec.pRequest->sqlstr) {
6!
1015
      taosMemoryFreeClear(pStmt->exec.pRequest->sqlstr);
6!
1016
    }
1017
    pStmt->exec.pRequest->sqlstr = newSql;
6✔
1018
    pStmt->exec.pRequest->sqlLen = strlen(newSql);
6✔
1019

1020
    if (pStmt->sql.sqlStr) {
6!
1021
      taosMemoryFreeClear(pStmt->sql.sqlStr);
6!
1022
    }
1023
    pStmt->sql.sqlStr = taosStrndup(newSql, strlen(newSql));
6!
1024
    pStmt->sql.sqlLen = strlen(newSql);
6✔
1025
  }
1026

1027
  STMT_ERR_RET(code);
10,984✔
1028

1029
  char* dbName = NULL;
10,983✔
1030
  if (qParseDbName(sql, length, &dbName)) {
10,983✔
1031
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
885✔
1032
    taosMemoryFreeClear(dbName);
883!
1033
  }
1034

1035
  return TSDB_CODE_SUCCESS;
10,986✔
1036
}
1037

1038
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
76✔
1039
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
76✔
1040
  if (!pSrc) {
77!
1041
    return terrno;
×
1042
  }
1043
  STableDataCxt* pDst = NULL;
77✔
1044

1045
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
77!
1046
  pStmt->sql.siInfo.pDataCtx = pDst;
77✔
1047

1048
  SArray* pTblCols = NULL;
77✔
1049
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
70,941✔
1050
    pTblCols = taosArrayInit(20, POINTER_BYTES);
70,806✔
1051
    if (NULL == pTblCols) {
71,522!
1052
      return terrno;
×
1053
    }
1054

1055
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
142,386!
1056
      return terrno;
×
1057
    }
1058
  }
1059

1060
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
135✔
1061

1062
  return TSDB_CODE_SUCCESS;
135✔
1063
}
1064

1065
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
884✔
1066
  STscStmt* pStmt = (STscStmt*)stmt;
884✔
1067

1068
  STMT_DLOG("start to set dbName:%s", dbName);
884!
1069

1070
  STMT_ERR_RET(stmtCreateRequest(pStmt));
884!
1071

1072
  // The SQL statement specifies a database name, overriding the previously specified database
1073
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
883!
1074
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
885!
1075
  if (pStmt->exec.pRequest->pDb == NULL) {
885!
1076
    return terrno;
×
1077
  }
1078
  return TSDB_CODE_SUCCESS;
885✔
1079
}
1080

1081
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
15,685✔
1082
  STscStmt* pStmt = (STscStmt*)stmt;
15,685✔
1083

1084
  int64_t startUs = taosGetTimestampUs();
15,693✔
1085

1086
  STMT_DLOG("start to set tbName:%s", tbName);
15,693!
1087

1088
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,693✔
1089
    return pStmt->errCode;
24✔
1090
  }
1091

1092
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
15,669!
1093

1094
  int32_t insert = 0;
15,665✔
1095
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
15,665!
1096
  if (0 == insert) {
15,657!
1097
    tscError("set tb name not available for none insert statement");
×
1098
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1099
  }
1100

1101
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
15,657✔
1102
    STMT_ERR_RET(stmtCreateRequest(pStmt));
10,126!
1103

1104
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
10,128✔
1105
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1106
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
10,126!
1107

1108
    STMT_ERR_RET(stmtGetFromCache(pStmt));
10,121!
1109

1110
    if (pStmt->bInfo.needParse) {
10,120✔
1111
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
10,089✔
1112
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
10,089✔
1113

1114
      STMT_ERR_RET(stmtParseSql(pStmt));
10,089✔
1115
    }
1116
  } else {
1117
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
5,531✔
1118
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
5,531✔
1119
    pStmt->exec.pRequest->requestId++;
5,531✔
1120
    pStmt->bInfo.needParse = false;
5,531✔
1121
  }
1122

1123
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
15,637✔
1124
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
77!
1125
  }
1126

1127
  int64_t startUs2 = taosGetTimestampUs();
15,653✔
1128
  pStmt->stat.setTbNameUs += startUs2 - startUs;
15,653✔
1129

1130
  return TSDB_CODE_SUCCESS;
15,653✔
1131
}
1132

1133
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
15✔
1134
  STscStmt* pStmt = (STscStmt*)stmt;
15✔
1135

1136
  STMT_DLOG_E("start to set tbTags");
15!
1137

1138
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15✔
1139
    return pStmt->errCode;
4✔
1140
  }
1141

1142
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
11!
1143

1144
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
11✔
1145
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
11!
1146
    tscWarn("no tags bound in sql, will not bound tags");
×
1147
    return TSDB_CODE_SUCCESS;
×
1148
  }
1149

1150
  if (pStmt->bInfo.inExecCache) {
11!
1151
    return TSDB_CODE_SUCCESS;
×
1152
  }
1153

1154
  STableDataCxt** pDataBlock =
1155
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1156
  if (NULL == pDataBlock) {
11!
1157
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1158
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1159
  }
1160

1161
  tscDebug("start to bind stmt tag values");
11!
1162
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
11!
1163
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1164
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1165

1166
  return TSDB_CODE_SUCCESS;
11✔
1167
}
1168

1169
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2✔
1170
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2✔
1171
    return pStmt->errCode;
1✔
1172
  }
1173

1174
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1175
    tscError("invalid operation to get query tag fileds");
×
1176
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1177
  }
1178

1179
  STableDataCxt** pDataBlock =
1180
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1✔
1181
  if (NULL == pDataBlock) {
1!
1182
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1183
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1184
  }
1185

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

1188
  return TSDB_CODE_SUCCESS;
1✔
1189
}
1190

1191
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
11✔
1192
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11!
1193
    return pStmt->errCode;
×
1194
  }
1195

1196
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
11!
1197
    tscError("invalid operation to get query column fileds");
×
1198
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1199
  }
1200

1201
  STableDataCxt** pDataBlock = NULL;
11✔
1202

1203
  if (pStmt->sql.stbInterlaceMode) {
11!
1204
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1205
  } else {
1206
    pDataBlock =
1207
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1208
    if (NULL == pDataBlock) {
11!
1209
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1210
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1211
    }
1212
  }
1213
  if (pStmt->sql.predicateCols) {
11✔
1214
    STMT_ERR_RET(qBuildUpdateStmtColFields(*pDataBlock, fieldNum, fields, pStmt->sql.predicateCols));
5!
1215
  } else {
1216
    STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
6!
1217
  }
1218

1219
  return TSDB_CODE_SUCCESS;
11✔
1220
}
1221

1222
/*
1223
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1224
  while (true) {
1225
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1226
      pStmt->exec.smInfo.pColIdx = 0;
1227
    }
1228

1229
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1230
      taosUsleep(1);
1231
      continue;
1232
    }
1233

1234
    *idx = pStmt->exec.smInfo.pColIdx;
1235
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1236
  }
1237
}
1238
*/
1239

1240
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
5,627✔
1241
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,627✔
1242
    pStmt->sql.siInfo.pVgroupHash =
4,555✔
1243
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,554✔
1244
  }
1245
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,628✔
1246
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,554✔
1247
  }
1248

1249
  if (NULL == pStmt->sql.siInfo.pRequest) {
5,629✔
1250
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
77!
1251
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1252

1253
    if (pStmt->reqid != 0) {
77!
1254
      pStmt->reqid++;
×
1255
    }
1256
    pStmt->exec.pRequest->syncQuery = true;
77✔
1257

1258
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
77✔
1259
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
77✔
1260
  }
1261

1262
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
5,629✔
1263
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
372✔
1264
    pStmt->sql.siInfo.tbFromHash = true;
22✔
1265
  }
1266

1267
  if (0 == pStmt->sql.siInfo.firstName[0]) {
5,629✔
1268
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
77✔
1269
  }
1270

1271
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,629✔
1272
  param->next = NULL;
5,629✔
1273

1274
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,629✔
1275

1276
  stmtEnqueue(pStmt, param);
5,629✔
1277

1278
  return TSDB_CODE_SUCCESS;
5,630✔
1279
}
1280

1281
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1282
  while (true) {
1283
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
5,628!
1284
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,629✔
1285
      break;
5,629✔
1286
    } else {
1287
      SArray* pTblCols = NULL;
×
1288
      for (int32_t i = 0; i < 100; i++) {
×
1289
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1290
        if (NULL == pTblCols) {
×
1291
          return terrno;
×
1292
        }
1293

1294
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1295
          return terrno;
×
1296
        }
1297
      }
1298
    }
1299
  }
1300

1301
  return TSDB_CODE_SUCCESS;
5,629✔
1302
}
1303

1304
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
86,306✔
1305
  STscStmt* pStmt = (STscStmt*)stmt;
86,306✔
1306
  int32_t   code = 0;
86,306✔
1307

1308
  int64_t startUs = taosGetTimestampUs();
86,279✔
1309

1310
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
86,279!
1311

1312
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
86,376✔
1313
    return pStmt->errCode;
84✔
1314
  }
1315

1316
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
86,292!
1317

1318
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
86,133!
1319
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1320
    pStmt->bInfo.needParse = false;
×
1321
  }
1322

1323
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
86,133!
1324
    taos_free_result(pStmt->exec.pRequest);
×
1325
    pStmt->exec.pRequest = NULL;
×
1326
  }
1327

1328
  STMT_ERR_RET(stmtCreateRequest(pStmt));
86,133!
1329

1330
  if (pStmt->bInfo.needParse) {
86,032✔
1331
    STMT_ERR_RET(stmtParseSql(pStmt));
836✔
1332
  }
1333

1334
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
86,051✔
1335
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
2!
1336

1337
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
2✔
1338
                         .acctId = pStmt->taos->acctId,
2✔
1339
                         .db = pStmt->exec.pRequest->pDb,
2✔
1340
                         .topicQuery = false,
1341
                         .pSql = pStmt->sql.sqlStr,
2✔
1342
                         .sqlLen = pStmt->sql.sqlLen,
2✔
1343
                         .pMsg = pStmt->exec.pRequest->msgBuf,
2✔
1344
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1345
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
2✔
1346
                         .pStmtCb = NULL,
1347
                         .pUser = pStmt->taos->user,
2✔
1348
                         .setQueryFp = setQueryRequest,
1349
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
2✔
1350

1351
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2✔
1352
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
2!
1353

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

1356
    if (pStmt->sql.pQuery->haveResultSet) {
2!
1357
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
2!
1358
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1359
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
2!
1360
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
2!
1361
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
2✔
1362
    }
1363

1364
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
2✔
1365
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
2✔
1366
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
2✔
1367

1368
    return TSDB_CODE_SUCCESS;
2✔
1369
  }
1370

1371
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
86,049!
1372
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1373
  }
1374

1375
  STableDataCxt** pDataBlock = NULL;
85,980✔
1376

1377
  if (pStmt->exec.pCurrBlock) {
85,980✔
1378
    pDataBlock = &pStmt->exec.pCurrBlock;
75,060✔
1379
  } else {
1380
    pDataBlock =
1381
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,920✔
1382
    if (NULL == pDataBlock) {
10,929!
1383
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1384
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1385
    }
1386
    pStmt->exec.pCurrBlock = *pDataBlock;
10,929✔
1387
    if (pStmt->sql.stbInterlaceMode) {
10,929✔
1388
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
77✔
1389
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
77✔
1390
    }
1391
  }
1392

1393
  int64_t startUs2 = taosGetTimestampUs();
85,959✔
1394
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
85,959✔
1395

1396
  SStmtQNode* param = NULL;
85,959✔
1397
  if (pStmt->sql.stbInterlaceMode) {
85,959✔
1398
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,252!
1399
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,257!
1400
    taosArrayClear(param->tblData.aCol);
5,629✔
1401

1402
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1403

1404
    param->restoreTbCols = false;
5,630✔
1405
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,630✔
1406
  }
1407

1408
  int64_t startUs3 = taosGetTimestampUs();
85,947✔
1409
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
85,947✔
1410

1411
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
85,947✔
1412

1413
  if (pStmt->sql.predicateCols) {
85,947✔
1414
    STMT_ERR_RET(qBindUpdateStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
5!
1415
                                          pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt,
1416
                                          pStmt->sql.predicateCols));
1417
    return TSDB_CODE_SUCCESS;
5✔
1418
  }
1419

1420
  if (colIdx < 0) {
85,942!
1421
    if (pStmt->sql.stbInterlaceMode) {
86,019✔
1422
      (*pDataBlock)->pData->flags = 0;
5,628✔
1423
      code =
1424
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
5,628✔
1425
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
5,628✔
1426
    } else {
1427
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
80,391✔
1428
                                pStmt->taos->optionInfo.charsetCxt);
80,391✔
1429
    }
1430

1431
    if (code) {
86,197✔
1432
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1433
      STMT_ERR_RET(code);
1!
1434
    }
1435
  } else {
1436
    if (pStmt->sql.stbInterlaceMode) {
×
1437
      tscError("bind single column not allowed in stb insert mode");
×
1438
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1439
    }
1440

1441
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1442
      tscError("bind column index not in sequence");
×
1443
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1444
    }
1445

1446
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1447

1448
    if (0 == colIdx) {
×
1449
      pStmt->bInfo.sBindRowNum = bind->num;
×
1450
    }
1451

1452
    code =
1453
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
1454
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1455
    if (code) {
×
1456
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1457
      STMT_ERR_RET(code);
×
1458
    }
1459
  }
1460

1461
  int64_t startUs4 = taosGetTimestampUs();
85,876✔
1462
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
85,876✔
1463

1464
  if (pStmt->sql.stbInterlaceMode) {
85,876✔
1465
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,627!
1466
  }
1467

1468
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
85,995✔
1469

1470
  return TSDB_CODE_SUCCESS;
85,995✔
1471
}
1472

1473
int stmtAddBatch(TAOS_STMT* stmt) {
84,618✔
1474
  STscStmt* pStmt = (STscStmt*)stmt;
84,618✔
1475

1476
  int64_t startUs = taosGetTimestampUs();
84,602✔
1477

1478
  STMT_DLOG_E("start to add batch");
84,602!
1479

1480
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
84,532✔
1481
    return pStmt->errCode;
30✔
1482
  }
1483

1484
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
84,502!
1485

1486
  if (pStmt->sql.stbInterlaceMode) {
84,624✔
1487
    int64_t startUs2 = taosGetTimestampUs();
4,555✔
1488
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,555✔
1489

1490
    pStmt->sql.siInfo.tableColsReady = false;
4,555✔
1491

1492
    SStmtQNode* param = NULL;
4,555✔
1493
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
9,110!
1494
    param->restoreTbCols = true;
4,555✔
1495
    param->next = NULL;
4,555✔
1496

1497
    stmtEnqueue(pStmt, param);
4,555✔
1498

1499
    return TSDB_CODE_SUCCESS;
4,555✔
1500
  }
1501

1502
  STMT_ERR_RET(stmtCacheBlock(pStmt));
80,069!
1503

1504
  return TSDB_CODE_SUCCESS;
80,021✔
1505
}
1506
/*
1507
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1508
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1509

1510
  int32_t code = 0;
1511
  int32_t finalCode = 0;
1512
  size_t  keyLen = 0;
1513
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1514
  while (pIter) {
1515
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1516
    char*          key = taosHashGetKey(pIter, &keyLen);
1517

1518
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1519
    if (pMeta->uid) {
1520
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1521
      continue;
1522
    }
1523

1524
    SSubmitBlkRsp* blkRsp = NULL;
1525
    int32_t        i = 0;
1526
    for (; i < pRsp->nBlocks; ++i) {
1527
      blkRsp = pRsp->pBlocks + i;
1528
      if (strlen(blkRsp->tblFName) != keyLen) {
1529
        continue;
1530
      }
1531

1532
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1533
        continue;
1534
      }
1535

1536
      break;
1537
    }
1538

1539
    if (i < pRsp->nBlocks) {
1540
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1541
               blkRsp->uid);
1542

1543
      pMeta->uid = blkRsp->uid;
1544
      pStmt->bInfo.tbUid = blkRsp->uid;
1545
    } else {
1546
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1547
      if (NULL == pStmt->pCatalog) {
1548
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1549
        if (code) {
1550
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1551
          finalCode = code;
1552
          continue;
1553
        }
1554
      }
1555

1556
      code = stmtCreateRequest(pStmt);
1557
      if (code) {
1558
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1559
        finalCode = code;
1560
        continue;
1561
      }
1562

1563
      STableMeta*      pTableMeta = NULL;
1564
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1565
                               .requestId = pStmt->exec.pRequest->requestId,
1566
                               .requestObjRefId = pStmt->exec.pRequest->self,
1567
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1568
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1569

1570
      pStmt->stat.ctgGetTbMetaNum++;
1571

1572
      taos_free_result(pStmt->exec.pRequest);
1573
      pStmt->exec.pRequest = NULL;
1574

1575
      if (code || NULL == pTableMeta) {
1576
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1577
        finalCode = code;
1578
        taosMemoryFree(pTableMeta);
1579
        continue;
1580
      }
1581

1582
      pMeta->uid = pTableMeta->uid;
1583
      pStmt->bInfo.tbUid = pTableMeta->uid;
1584
      taosMemoryFree(pTableMeta);
1585
    }
1586

1587
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1588
  }
1589

1590
  return finalCode;
1591
}
1592
*/
1593

1594
/*
1595
int stmtStaticModeExec(TAOS_STMT* stmt) {
1596
  STscStmt*   pStmt = (STscStmt*)stmt;
1597
  int32_t     code = 0;
1598
  SSubmitRsp* pRsp = NULL;
1599
  if (pStmt->sql.staticMode) {
1600
    return TSDB_CODE_TSC_STMT_API_ERROR;
1601
  }
1602

1603
  STMT_DLOG_E("start to exec");
1604

1605
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1606

1607
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1608
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1609

1610
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1611

1612
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1613
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1614
    if (code) {
1615
      pStmt->exec.pRequest->code = code;
1616
    } else {
1617
      tFreeSSubmitRsp(pRsp);
1618
      STMT_ERR_RET(stmtResetStmt(pStmt));
1619
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1620
    }
1621
  }
1622

1623
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1624

1625
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1626
  pStmt->affectedRows += pStmt->exec.affectedRows;
1627

1628
_return:
1629

1630
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1631

1632
  tFreeSSubmitRsp(pRsp);
1633

1634
  ++pStmt->sql.runTimes;
1635

1636
  STMT_RET(code);
1637
}
1638
*/
1639

1640
int stmtExec(TAOS_STMT* stmt) {
15,446✔
1641
  STscStmt*   pStmt = (STscStmt*)stmt;
15,446✔
1642
  int32_t     code = 0;
15,446✔
1643
  SSubmitRsp* pRsp = NULL;
15,446✔
1644

1645
  int64_t startUs = taosGetTimestampUs();
15,458✔
1646

1647
  STMT_DLOG_E("start to exec");
15,458!
1648

1649
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,462✔
1650
    return pStmt->errCode;
30✔
1651
  }
1652

1653
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
15,432✔
1654

1655
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,413✔
1656
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2✔
1657
  } else {
1658
    if (pStmt->sql.stbInterlaceMode) {
15,411✔
1659
      int64_t startTs = taosGetTimestampUs();
4,554✔
1660
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
10,891✔
1661
        taosUsleep(1);
6,338✔
1662
      }
1663
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,555✔
1664

1665
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,555!
1666
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,554✔
1667
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,555✔
1668
      pStmt->sql.siInfo.pVgroupList = NULL;
4,555✔
1669
    } else {
1670
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,857✔
1671
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,863!
1672

1673
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
10,863!
1674

1675
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
10,870!
1676
    }
1677

1678
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
15,413✔
1679
  }
1680

1681
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
15,434!
1682
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2✔
1683
    if (code) {
×
1684
      pStmt->exec.pRequest->code = code;
×
1685
    } else {
1686
      tFreeSSubmitRsp(pRsp);
×
1687
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1688
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1689
    }
1690
  }
1691

1692
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
15,432!
1693

1694
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
15,432✔
1695
  pStmt->affectedRows += pStmt->exec.affectedRows;
15,440✔
1696

1697
_return:
15,440✔
1698

1699
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
15,440!
1700
    taosUsleep(1);
×
1701
  }
1702

1703
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
15,438!
1704

1705
  tFreeSSubmitRsp(pRsp);
15,435✔
1706

1707
  ++pStmt->sql.runTimes;
15,431✔
1708

1709
  int64_t startUs2 = taosGetTimestampUs();
15,439✔
1710
  pStmt->stat.execUseUs += startUs2 - startUs;
15,439✔
1711

1712
  STMT_RET(code);
15,439!
1713
}
1714

1715
int stmtClose(TAOS_STMT* stmt) {
994✔
1716
  STscStmt* pStmt = (STscStmt*)stmt;
994✔
1717

1718
  STMT_DLOG_E("start to free stmt");
994!
1719

1720
  if (pStmt->bindThreadInUse) {
994✔
1721
    pStmt->queue.stopQueue = true;
95✔
1722

1723
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
95✔
1724
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
95✔
1725
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
95✔
1726
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
95✔
1727

1728
    (void)taosThreadJoin(pStmt->bindThread, NULL);
95✔
1729
    pStmt->bindThreadInUse = false;
95✔
1730

1731
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
95✔
1732
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
95✔
1733
  }
1734

1735
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
993!
1736
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1737
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1738
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1739
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1740
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1741
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1742
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1743
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1744
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1745

1746
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
993!
1747
  taosMemoryFree(stmt);
996!
1748

1749
  return TSDB_CODE_SUCCESS;
997✔
1750
}
1751

1752
const char* stmtErrstr(TAOS_STMT* stmt) {
189✔
1753
  STscStmt* pStmt = (STscStmt*)stmt;
189✔
1754

1755
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
189!
1756
    return (char*)tstrerror(terrno);
×
1757
  }
1758

1759
  pStmt->exec.pRequest->code = terrno;
189✔
1760

1761
  return taos_errstr(pStmt->exec.pRequest);
189✔
1762
}
1763

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

1766
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
56✔
1767

1768
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
101,789✔
1769
  STscStmt* pStmt = (STscStmt*)stmt;
101,789✔
1770

1771
  STMT_DLOG_E("start is insert");
101,789!
1772

1773
  if (pStmt->sql.type) {
101,897✔
1774
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
90,963!
1775
  } else {
1776
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
10,934✔
1777
  }
1778

1779
  return TSDB_CODE_SUCCESS;
101,900✔
1780
}
1781

1782
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
6✔
1783
  int32_t   code = 0;
6✔
1784
  STscStmt* pStmt = (STscStmt*)stmt;
6✔
1785
  int32_t   preCode = pStmt->errCode;
6✔
1786

1787
  STMT_DLOG_E("start to get tag fields");
6!
1788

1789
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6!
1790
    return pStmt->errCode;
×
1791
  }
1792

1793
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6!
1794
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1795
  }
1796

1797
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
6!
1798

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

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

1809
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
6!
1810

1811
  if (pStmt->bInfo.needParse) {
6✔
1812
    STMT_ERRI_JRET(stmtParseSql(pStmt));
5✔
1813
  }
1814

1815
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
2✔
1816

1817
_return:
1✔
1818
  // compatible with previous versions
1819
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
6!
1820
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
1821
  }
1822

1823
  if (code != TSDB_CODE_SUCCESS) {
6✔
1824
    taos_free_result(pStmt->exec.pRequest);
5✔
1825
    pStmt->exec.pRequest = NULL;
5✔
1826
  }
1827
  pStmt->errCode = preCode;
6✔
1828

1829
  return code;
6✔
1830
}
1831

1832
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
7✔
1833
  int32_t   code = 0;
7✔
1834
  STscStmt* pStmt = (STscStmt*)stmt;
7✔
1835
  int32_t   preCode = pStmt->errCode;
7✔
1836

1837
  STMT_DLOG_E("start to get col fields");
7!
1838

1839
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7✔
1840
    return pStmt->errCode;
1✔
1841
  }
1842

1843
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6!
1844
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1845
  }
1846

1847
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
6!
1848

1849
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
6!
1850
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1851
    pStmt->bInfo.needParse = false;
×
1852
  }
1853

1854
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
6!
1855
    taos_free_result(pStmt->exec.pRequest);
×
1856
    pStmt->exec.pRequest = NULL;
×
1857
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1858
  }
1859

1860
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
6!
1861

1862
  if (pStmt->bInfo.needParse) {
6✔
1863
    STMT_ERRI_JRET(stmtParseSql(pStmt));
5!
1864
  }
1865

1866
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
6!
1867

1868
_return:
6✔
1869

1870
  if (code != TSDB_CODE_SUCCESS) {
6!
1871
    taos_free_result(pStmt->exec.pRequest);
×
1872
    pStmt->exec.pRequest = NULL;
×
1873
  }
1874

1875
  pStmt->errCode = preCode;
6✔
1876

1877
  return code;
6✔
1878
}
1879

1880
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
4✔
1881
  int       code = 0;
4✔
1882
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1883
  int32_t   preCode = pStmt->errCode;
4✔
1884

1885
  STMT_DLOG_E("start to get param num");
4!
1886

1887
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4✔
1888
    return pStmt->errCode;
1✔
1889
  }
1890

1891
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
3!
1892

1893
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
3!
1894
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1895
    pStmt->bInfo.needParse = false;
×
1896
  }
1897

1898
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
3!
1899
    taos_free_result(pStmt->exec.pRequest);
×
1900
    pStmt->exec.pRequest = NULL;
×
1901
  }
1902

1903
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
3!
1904

1905
  if (pStmt->bInfo.needParse) {
3✔
1906
    STMT_ERRI_JRET(stmtParseSql(pStmt));
2!
1907
  }
1908

1909
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1910
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1911
  } else {
1912
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1913
  }
1914

1915
_return:
1✔
1916

1917
  if (code != TSDB_CODE_SUCCESS) {
3✔
1918
    taos_free_result(pStmt->exec.pRequest);
2✔
1919
    pStmt->exec.pRequest = NULL;
2✔
1920
  }
1921

1922
  pStmt->errCode = preCode;
3✔
1923

1924
  return code;
3✔
1925
}
1926

1927
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
4✔
1928
  int       code = 0;
4✔
1929
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1930
  int32_t   preCode = pStmt->errCode;
4✔
1931

1932
  STMT_DLOG_E("start to get param");
4!
1933

1934
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1935
    return pStmt->errCode;
×
1936
  }
1937

1938
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1939
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1940
  }
1941

1942
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1943

1944
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1945
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1946
    pStmt->bInfo.needParse = false;
×
1947
  }
1948

1949
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1950
    taos_free_result(pStmt->exec.pRequest);
×
1951
    pStmt->exec.pRequest = NULL;
×
1952
  }
1953

1954
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1955

1956
  if (pStmt->bInfo.needParse) {
4!
1957
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1958
  }
1959

1960
  int32_t       nums = 0;
4✔
1961
  TAOS_FIELD_E* pField = NULL;
4✔
1962
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
4!
1963
  if (idx >= nums) {
4!
1964
    tscError("idx %d is too big", idx);
×
1965
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1966
  }
1967

1968
  *type = pField[idx].type;
4✔
1969
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
4✔
1970

1971
_return:
4✔
1972

1973
  if (code != TSDB_CODE_SUCCESS) {
4!
1974
    taos_free_result(pStmt->exec.pRequest);
×
1975
    pStmt->exec.pRequest = NULL;
×
1976
  }
1977

1978
  taosMemoryFree(pField);
4!
1979
  pStmt->errCode = preCode;
4✔
1980

1981
  return code;
4✔
1982
}
1983

1984
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
2✔
1985
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1986

1987
  STMT_DLOG_E("start to use result");
2!
1988

1989
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
2!
1990
    tscError("useResult only for query statement");
×
1991
    return NULL;
×
1992
  }
1993

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