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

taosdata / TDengine / #4828

29 Oct 2025 11:10AM UTC coverage: 61.071% (-0.3%) from 61.383%
#4828

push

travis-ci

web-flow
Merge pull request #33419 from taosdata/3.0

3.0

155924 of 324872 branches covered (48.0%)

Branch coverage included in aggregate %.

207404 of 270051 relevant lines covered (76.8%)

243478715.98 hits per line

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

66.98
/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) {
17,785,861!
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
17,788,065✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
17,786,886✔
17
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
172!
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;
17,784,528✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
17,789,539✔
44
  int i = 0;
17,789,539✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
94,824,590✔
46
    if (i < 10) {
77,041,112✔
47
      taosUsleep(1);
70,645,355✔
48
      i++;
70,639,294✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
6,395,757✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
6,394,578!
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
6,394,578✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
6,394,578✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
17,784,657✔
58
    return false;
110,815✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
17,675,021✔
61
  SStmtQNode* node = pStmt->queue.head->next;
17,675,021✔
62
  pStmt->queue.head = pStmt->queue.head->next;
17,675,021✔
63
  *param = node;
17,675,021✔
64

65
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
17,675,021✔
66

67
  return true;
17,676,294✔
68
}
69

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

74
  pStmt->stat.bindDataNum++;
17,676,089✔
75

76
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
17,677,268✔
77
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
17,678,584✔
78
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
17,678,376✔
79
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
17,678,168✔
80
}
17,676,641✔
81

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
263,632,032✔
83
  int32_t code = 0;
263,632,032✔
84

85
  if (pStmt->exec.pRequest == NULL) {
263,632,032✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
9,668,621✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
9,666,387!
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
9,667,786✔
92
      pStmt->exec.pRequest->syncQuery = true;
9,664,446✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
9,666,406✔
94
    }
95
  }
96

97
  return code;
263,307,873✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
505,104,564✔
101
  int32_t code = 0;
505,104,564✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
505,104,564!
104
    STMT_LOG_SEQ(newStatus);
510,895,052✔
105
  }
106

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

112
  switch (newStatus) {
510,088,686!
113
    case STMT_PREPARE:
9,613,657✔
114
      pStmt->errCode = 0;
9,613,657✔
115
      break;
8,912,622✔
116
    case STMT_SETTBNAME:
19,534,823✔
117
      if (STMT_STATUS_EQ(INIT)) {
19,534,823!
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
19,533,988!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
19,533,153✔
124
    case STMT_SETTAGS:
23,089✔
125
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
23,089!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
23,089✔
129
    case STMT_FETCH_FIELDS:
2,768✔
130
      if (STMT_STATUS_EQ(INIT)) {
2,768!
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      break;
2,768✔
134
    case STMT_BIND:
234,127,429✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
234,127,429✔
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;
234,485,852✔
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:
230,745,353✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
230,745,353!
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
231,374,971✔
154
    case STMT_EXECUTE:
16,041,567✔
155
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
16,041,567✔
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
27,018!
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)) {
16,013,272!
162
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
        }
164
      }
165
      break;
16,044,416✔
166
    default:
×
167
      code = TSDB_CODE_APP_ERROR;
×
168
      break;
×
169
  }
170

171
  STMT_ERR_RET(code);
510,359,617!
172

173
  pStmt->sql.status = newStatus;
510,359,617✔
174

175
  return TSDB_CODE_SUCCESS;
510,679,749✔
176
}
177

178
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
8,474,493✔
179
  STscStmt* pStmt = (STscStmt*)stmt;
8,474,493✔
180

181
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
8,474,493✔
182

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

188
  *tbName = pStmt->bInfo.tbName;
8,476,652✔
189

190
  return TSDB_CODE_SUCCESS;
8,475,817✔
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,
9,507,907✔
243
                           bool autoCreateTbl, uint8_t tbNameFlag) {
244
  STscStmt* pStmt = (STscStmt*)stmt;
9,507,907✔
245
  char      tbFName[TSDB_TABLE_FNAME_LEN];
9,460,748✔
246
  int32_t   code = tNameExtractFullName(tbName, tbFName);
9,513,190✔
247
  if (code != 0) {
9,517,356!
248
    return code;
×
249
  }
250

251
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
9,517,356!
252
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
9,513,181!
253
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
9,510,676✔
254

255
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
9,514,851✔
256
  pStmt->bInfo.tbSuid = pTableMeta->suid;
9,514,851✔
257
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
9,514,289✔
258
  pStmt->bInfo.tbType = pTableMeta->tableType;
9,511,511✔
259
  pStmt->bInfo.boundTags = tags;
9,516,503✔
260
  pStmt->bInfo.tagsCached = false;
9,516,945✔
261
  pStmt->bInfo.tbNameFlag = tbNameFlag;
9,515,244✔
262
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
9,508,669!
263

264
  return TSDB_CODE_SUCCESS;
9,499,379✔
265
}
266

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

270
  pStmt->sql.pVgHash = pVgHash;
9,503,987✔
271
  pStmt->exec.pBlockHash = pBlockHash;
9,514,297✔
272

273
  return TSDB_CODE_SUCCESS;
9,511,792✔
274
}
275

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

281
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
9,504,822!
282
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
9,498,980!
283

284
  pStmt->sql.autoCreateTbl = autoCreateTbl;
9,510,122✔
285
  if (pStmt->sql.autoCreateTbl) {
9,512,627✔
286
    pStmt->sql.stbInterlaceMode = false;
8,377,754✔
287
  }
288

289
  return TSDB_CODE_SUCCESS;
9,512,234✔
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) {
223,341,002✔
305
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
223,341,002✔
306
    return TSDB_CODE_SUCCESS;
216,159,634✔
307
  }
308

309
  uint64_t uid = pStmt->bInfo.tbUid;
8,417,616✔
310
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
8,418,451!
311

312
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
8,420,121✔
313
    return TSDB_CODE_SUCCESS;
47,831✔
314
  }
315

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

322
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
8,380,921!
323

324
  SStmtTableCache cache = {
8,378,416✔
325
      .pDataCtx = pDst,
326
      .boundTags = pStmt->bInfo.boundTags,
8,379,251✔
327
  };
328

329
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
8,380,086!
330
    return terrno;
×
331
  }
332

333
  if (pStmt->sql.autoCreateTbl) {
8,381,756!
334
    pStmt->bInfo.tagsCached = true;
8,380,748✔
335
  } else {
336
    pStmt->bInfo.boundTags = NULL;
173✔
337
  }
338

339
  return TSDB_CODE_SUCCESS;
8,378,416✔
340
}
341

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

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

352
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,552,538!
353
  pStmt->exec.pRequest->stmtBindVersion = 1;
9,547,640✔
354

355
  pStmt->stat.parseSqlNum++;
9,551,815✔
356
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
9,551,815✔
357
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
9,537,551✔
358

359
  pStmt->bInfo.needParse = false;
9,543,003✔
360

361
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
9,542,854✔
362
    pStmt->sql.type = STMT_TYPE_INSERT;
1,044,672✔
363
    pStmt->sql.stbInterlaceMode = false;
1,043,395✔
364
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
8,491,498✔
365
    pStmt->sql.type = STMT_TYPE_QUERY;
27,018✔
366
    pStmt->sql.stbInterlaceMode = false;
27,018✔
367

368
    return TSDB_CODE_SUCCESS;
27,018✔
369
  }
370

371
  STableDataCxt** pSrc =
372
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
9,514,708!
373
  if (NULL == pSrc || NULL == *pSrc) {
9,518,602!
374
    return terrno;
1,108✔
375
  }
376

377
  STableDataCxt* pTableCtx = *pSrc;
9,517,494✔
378
  if (pStmt->sql.stbInterlaceMode) {
9,512,484✔
379
    int16_t lastIdx = -1;
93,225✔
380

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

387
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
1,067,911✔
388
    }
389
  }
390

391
  if (NULL == pStmt->sql.pBindInfo) {
9,517,422✔
392
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
9,513,129!
393
    if (NULL == pStmt->sql.pBindInfo) {
9,512,569!
394
      return terrno;
×
395
    }
396
  }
397

398
  return TSDB_CODE_SUCCESS;
9,508,701✔
399
}
400

401
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
25,770,658✔
402
  pStmt->bInfo.tbUid = 0;
25,770,658✔
403
  pStmt->bInfo.tbSuid = 0;
25,773,016✔
404
  pStmt->bInfo.tbVgId = -1;
25,773,851✔
405
  pStmt->bInfo.tbType = 0;
25,767,853✔
406
  pStmt->bInfo.needParse = true;
25,769,823✔
407
  pStmt->bInfo.inExecCache = false;
25,771,002✔
408

409
  pStmt->bInfo.tbName[0] = 0;
25,772,181✔
410
  pStmt->bInfo.tbFName[0] = 0;
25,772,679✔
411
  if (!pStmt->bInfo.tagsCached) {
25,770,658✔
412
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
9,001,900✔
413
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
9,003,462!
414
  }
415
  pStmt->bInfo.stbFName[0] = 0;
25,768,231✔
416

417
  return TSDB_CODE_SUCCESS;
25,765,917✔
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) {
6,542,152✔
426
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
6,542,152✔
427
  if (NULL == pTblBuf->pCurBuff) {
6,543,539✔
428
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
531!
429
    return;
×
430
  }
431
  pTblBuf->buffIdx = 1;
6,543,008✔
432
  pTblBuf->buffOffset = sizeof(*pQueue->head);
6,543,008✔
433

434
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
6,541,829✔
435
  pQueue->qRemainNum = 0;
6,543,008✔
436
  pQueue->head->next = NULL;
6,543,008✔
437
}
438

439
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
25,649,474✔
440
  if (pStmt->sql.stbInterlaceMode) {
25,649,474✔
441
    if (deepClean) {
6,630,687✔
442
      taosHashCleanup(pStmt->exec.pBlockHash);
93,225✔
443
      pStmt->exec.pBlockHash = NULL;
93,225✔
444

445
      if (NULL != pStmt->exec.pCurrBlock) {
93,225!
446
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
93,225!
447
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
93,225✔
448
      }
449
    } else {
450
      pStmt->sql.siInfo.pTableColsIdx = 0;
6,537,462✔
451
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
6,538,634✔
452
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
6,542,001✔
453
    }
454
  } else {
455
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
19,020,457✔
456
      taos_free_result(pStmt->exec.pRequest);
18,993,937✔
457
      pStmt->exec.pRequest = NULL;
18,994,145✔
458
    }
459

460
    size_t keyLen = 0;
19,022,833✔
461
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
19,022,833✔
462
    while (pIter) {
37,936,134✔
463
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
18,913,163✔
464
      char*          key = taosHashGetKey(pIter, &keyLen);
18,913,163✔
465
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
18,913,163✔
466

467
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
18,911,493✔
468
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
9,473,911✔
469
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
9,526,087!
470

471
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
9,473,773✔
472
        continue;
9,473,076✔
473
      }
474

475
      qDestroyStmtDataBlock(pBlocks);
9,437,582✔
476
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
9,438,279!
477

478
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
9,436,609✔
479
    }
480

481
    if (keepTable) {
19,022,971✔
482
      return TSDB_CODE_SUCCESS;
9,500,094✔
483
    }
484

485
    taosHashCleanup(pStmt->exec.pBlockHash);
9,522,877✔
486
    pStmt->exec.pBlockHash = NULL;
9,522,739✔
487

488
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
9,522,739✔
489
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
9,522,877!
490
  }
491

492
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
16,158,265!
493

494
  return TSDB_CODE_SUCCESS;
16,154,316✔
495
}
496

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

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

507
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
9,610,835✔
508
  STMT_DLOG_E("start to free SQL info");
9,610,835✔
509

510
  taosMemoryFree(pStmt->sql.pBindInfo);
9,610,835!
511
  taosMemoryFree(pStmt->sql.queryRes.fields);
9,610,429!
512
  taosMemoryFree(pStmt->sql.queryRes.userFields);
9,614,380!
513
  taosMemoryFree(pStmt->sql.sqlStr);
9,612,013!
514
  qDestroyQuery(pStmt->sql.pQuery);
9,612,099✔
515
  taosArrayDestroy(pStmt->sql.nodeList);
9,612,663✔
516
  taosHashCleanup(pStmt->sql.pVgHash);
9,614,604✔
517
  pStmt->sql.pVgHash = NULL;
9,613,498✔
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
9,612,663✔
520
  while (pIter) {
17,994,552✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
8,380,921✔
522

523
    qDestroyStmtDataBlock(pCache->pDataCtx);
8,380,921✔
524
    qDestroyBoundColInfo(pCache->boundTags);
8,382,591✔
525
    taosMemoryFreeClear(pCache->boundTags);
8,377,581!
526

527
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
8,380,086✔
528
  }
529
  taosHashCleanup(pStmt->sql.pTableCache);
9,613,631✔
530
  pStmt->sql.pTableCache = NULL;
9,613,769✔
531

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
9,614,604!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
9,613,769!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
9,614,604✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
9,614,466✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
9,614,328✔
538
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
9,613,631✔
539
  tSimpleHashCleanup(pStmt->sql.predicateCols);
9,614,466✔
540
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
9,613,631✔
541
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
9,614,466!
542
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
9,614,328✔
543
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
9,613,340✔
544

545
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
9,612,505!
546
  pStmt->sql.siInfo.tableColsReady = true;
9,612,505✔
547

548
  STMT_DLOG_E("end to free SQL info");
9,612,505✔
549

550
  return TSDB_CODE_SUCCESS;
9,613,407✔
551
}
552

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

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

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

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

575
  *vgId = vgInfo.vgId;
9,324✔
576

577
  return TSDB_CODE_SUCCESS;
9,324✔
578
}
579

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

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

587
  return TSDB_CODE_SUCCESS;
10,535✔
588
}
589

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

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

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

607
  return TSDB_CODE_SUCCESS;
1,730✔
608
}
609

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

617
  pStmt->bInfo.needParse = true;
8,497,643✔
618
  pStmt->bInfo.inExecCache = false;
8,497,299✔
619

620
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
8,503,832!
621
  if (pCxtInExec) {
8,505,555✔
622
    pStmt->bInfo.needParse = false;
18,480✔
623
    pStmt->bInfo.inExecCache = true;
18,480✔
624

625
    pStmt->exec.pCurrBlock = *pCxtInExec;
18,480✔
626

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

633
  if (NULL == pStmt->pCatalog) {
8,487,075✔
634
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
126,432!
635
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
126,485✔
636
  }
637

638
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
8,485,458✔
639
    if (pStmt->bInfo.inExecCache) {
8,480,715!
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);
8,477,375✔
646
    return TSDB_CODE_SUCCESS;
8,476,540✔
647
  }
648

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

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

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

663
      pStmt->exec.pCurrBlock = pNewBlock;
9,151✔
664

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

667
      return TSDB_CODE_SUCCESS;
9,151✔
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;
1,437✔
678
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1,437✔
679
                           .requestId = pStmt->exec.pRequest->requestId,
1,437✔
680
                           .requestObjRefId = pStmt->exec.pRequest->self,
1,437✔
681
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1,437✔
682
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1,384✔
683

684
  pStmt->stat.ctgGetTbMetaNum++;
1,384✔
685

686
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
1,384!
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);
1,384!
694

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

701
  taosMemoryFree(pTableMeta);
1,384!
702

703
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
1,384!
704

705
  if (uid == pStmt->bInfo.tbUid) {
1,384!
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) {
1,384!
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));
1,384✔
736
  if (pCache) {
1,384!
737
    pStmt->bInfo.needParse = false;
1,384✔
738

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

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

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

753
    pStmt->exec.pCurrBlock = pNewBlock;
1,384✔
754

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

757
    return TSDB_CODE_SUCCESS;
1,384✔
758
  }
759

760
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
761

762
  return TSDB_CODE_SUCCESS;
×
763
}
764

765
int32_t stmtResetStmt(STscStmt* pStmt) {
8,349,912✔
766
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
8,349,912!
767

768
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
8,349,912✔
769
  if (NULL == pStmt->sql.pTableCache) {
8,350,747!
770
    STMT_ERR_RET(terrno);
×
771
  }
772

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

777
  pStmt->sql.status = STMT_INIT;
8,350,747✔
778

779
  return TSDB_CODE_SUCCESS;
8,349,912✔
780
}
781

782
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
17,677,265✔
783
  SStmtQNode* pParam = (SStmtQNode*)param;
17,677,265✔
784

785
  if (pParam->restoreTbCols) {
17,677,265✔
786
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
17,676,953✔
787
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
11,133,260✔
788
      *p = taosArrayInit(20, POINTER_BYTES);
11,133,260✔
789
      if (*p == NULL) {
11,131,977!
790
        STMT_ERR_RET(terrno);
×
791
      }
792
    }
793

794
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
6,543,693✔
795
  } else {
796
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
11,134,647!
797
                                        &pStmt->sql.siInfo));
798

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

801
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
11,134,511✔
802
  }
803
  return TSDB_CODE_SUCCESS;
17,679,172✔
804
}
805

806
void* stmtBindThreadFunc(void* param) {
110,815✔
807
  setThreadName("stmtBind");
110,815✔
808

809
  qInfo("stmt bind thread started");
110,815!
810

811
  STscStmt* pStmt = (STscStmt*)param;
110,815✔
812

813
  while (true) {
17,788,636✔
814
    if (pStmt->queue.stopQueue) {
17,899,451✔
815
      break;
110,815✔
816
    }
817

818
    SStmtQNode* asyncParam = NULL;
17,788,636✔
819
    if (!stmtDequeue(pStmt, &asyncParam)) {
17,788,636✔
820
      continue;
110,815✔
821
    }
822

823
    int ret = stmtAsyncOutput(pStmt, asyncParam);
17,678,652✔
824
    if (ret != 0) {
17,679,000!
825
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
826
    }
827
  }
828

829
  qInfo("stmt bind thread stopped");
110,815!
830

831
  return NULL;
110,815✔
832
}
833

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

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

848
  pStmt->bindThreadInUse = true;
110,815✔
849

850
  (void)taosThreadAttrDestroy(&thAttr);
110,815✔
851
  return TSDB_CODE_SUCCESS;
110,815✔
852
}
853

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

860
  return TSDB_CODE_SUCCESS;
110,815✔
861
}
862

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

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

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

883
  return TSDB_CODE_SUCCESS;
110,815✔
884
}
885

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

891
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
1,260,580!
892
  if (NULL == pStmt) {
1,261,745!
893
    return NULL;
×
894
  }
895

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

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

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

915
  if (pStmt->stbInterlaceMode) {
1,262,884✔
916
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
110,815✔
917
    pStmt->sql.siInfo.acctId = taos->acctId;
110,815✔
918
    pStmt->sql.siInfo.dbname = taos->db;
110,815✔
919
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
110,815✔
920
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
110,815✔
921
    if (NULL == pStmt->sql.siInfo.pTableHash) {
110,815!
922
      (void)stmtClose(pStmt);
×
923
      return NULL;
×
924
    }
925
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
110,815✔
926
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
110,815!
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);
110,815✔
932
    if (NULL == pStmt->sql.siInfo.pTableCols) {
110,815!
933
      (void)stmtClose(pStmt);
×
934
      return NULL;
×
935
    }
936

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

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

953
  STMT_LOG_SEQ(STMT_INIT);
1,262,884✔
954

955
  tscDebug("stmt:%p initialized", pStmt);
1,262,884✔
956

957
  return pStmt;
1,262,884✔
958
}
959

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

963
  STMT_DLOG_E("start to prepare");
9,609,683✔
964

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

969
  if (pStmt->sql.status >= STMT_PREPARE) {
9,614,604✔
970
    STMT_ERR_RET(stmtResetStmt(pStmt));
8,351,582!
971
  }
972

973
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
9,612,934!
974

975
  if (length <= 0) {
9,612,532✔
976
    length = strlen(sql);
1,102,858!
977
  }
978

979
  pStmt->sql.sqlStr = taosStrndup(sql, length);
9,612,532!
980
  if (!pStmt->sql.sqlStr) {
9,614,492!
981
    return terrno;
×
982
  }
983
  pStmt->sql.sqlLen = length;
9,613,367✔
984
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
9,613,367!
985

986
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,613,367!
987

988
  int32_t code = 0;
9,606,528✔
989
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
19,169,400!
990
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
19,168,618✔
991
    // get table meta
992
    STableMeta* pTableMeta = NULL;
1,903✔
993
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
2,595✔
994

995
    char* newSql = NULL;
1,730✔
996

997
    // conver update sql to insert sql
998
    pStmt->sql.predicateCols = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
1,730✔
999
    if (NULL == pStmt->sql.predicateCols) {
1,730!
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,
3,460✔
1005
                                 pStmt->exec.pRequest->msgBufLen);
1,730✔
1006
    taosMemoryFree(pTableMeta);
1,730!
1007

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

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

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

1027
  STMT_ERR_RET(code);
9,606,716✔
1028

1029
  char* dbName = NULL;
9,606,543✔
1030
  if (qParseDbName(sql, length, &dbName)) {
9,606,543✔
1031
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
1,093,292✔
1032
    taosMemoryFreeClear(dbName);
1,090,037!
1033
  }
1034

1035
  return TSDB_CODE_SUCCESS;
9,608,213✔
1036
}
1037

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

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

1048
  SArray* pTblCols = NULL;
93,225✔
1049
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
92,827,463✔
1050
    pTblCols = taosArrayInit(20, POINTER_BYTES);
92,734,994✔
1051
    if (NULL == pTblCols) {
92,335,151!
1052
      return terrno;
×
1053
    }
1054

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

1060
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
92,469✔
1061

1062
  return TSDB_CODE_SUCCESS;
92,469✔
1063
}
1064

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

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

1070
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,092,167!
1071

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

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

1084
  int64_t startUs = taosGetTimestampUs();
19,541,897✔
1085

1086
  STMT_DLOG("start to set tbName:%s", tbName);
19,541,897✔
1087

1088
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
19,542,001!
1089
    return pStmt->errCode;
×
1090
  }
1091

1092
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
19,540,331!
1093

1094
  int32_t insert = 0;
19,537,978✔
1095
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
19,535,129!
1096
  if (0 == insert) {
19,536,795!
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) {
19,536,795✔
1102
    STMT_ERR_RET(stmtCreateRequest(pStmt));
8,497,425!
1103

1104
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
8,504,832!
1105
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1106
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
8,500,492!
1107

1108
    STMT_ERR_RET(stmtGetFromCache(pStmt));
8,497,643!
1109

1110
    if (pStmt->bInfo.needParse) {
8,503,050✔
1111
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
8,470,695!
1112
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
8,470,351✔
1113

1114
      STMT_ERR_RET(stmtParseSql(pStmt));
8,471,530✔
1115
    }
1116
  } else {
1117
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
11,031,995!
1118
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
11,030,809✔
1119
    pStmt->exec.pRequest->requestId++;
11,030,809✔
1120
    pStmt->bInfo.needParse = false;
11,033,167✔
1121
  }
1122

1123
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
19,526,174✔
1124
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
93,225!
1125
  }
1126

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

1130
  return TSDB_CODE_SUCCESS;
19,533,735✔
1131
}
1132

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

1136
  STMT_DLOG_E("start to set tbTags");
23,089✔
1137

1138
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
23,089!
1139
    return pStmt->errCode;
×
1140
  }
1141

1142
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
23,089!
1143

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

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

1154
  STableDataCxt** pDataBlock =
1155
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
23,089!
1156
  if (NULL == pDataBlock) {
23,089!
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");
23,089✔
1162
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
23,089!
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;
23,089✔
1167
}
1168

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

1174
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
173!
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));
173!
1181
  if (NULL == pDataBlock) {
173!
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));
173!
1187

1188
  return TSDB_CODE_SUCCESS;
173✔
1189
}
1190

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

1196
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,903!
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;
1,903✔
1202

1203
  if (pStmt->sql.stbInterlaceMode) {
1,903!
1204
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1205
  } else {
1206
    pDataBlock =
1207
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1,903!
1208
    if (NULL == pDataBlock) {
1,903!
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) {
1,903✔
1214
    STMT_ERR_RET(qBuildUpdateStmtColFields(*pDataBlock, fieldNum, fields, pStmt->sql.predicateCols));
865!
1215
  } else {
1216
    STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
1,038!
1217
  }
1218

1219
  return TSDB_CODE_SUCCESS;
1,903✔
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) {
11,129,993✔
1241
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
11,129,993✔
1242
    pStmt->sql.siInfo.pVgroupHash =
6,543,589✔
1243
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
6,541,231✔
1244
  }
1245
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
11,134,709✔
1246
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
6,543,693✔
1247
  }
1248

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

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

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

1262
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
11,132,265✔
1263
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
1,429,038!
1264
    pStmt->sql.siInfo.tbFromHash = true;
26,071✔
1265
  }
1266

1267
  if (0 == pStmt->sql.siInfo.firstName[0]) {
11,132,265✔
1268
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
93,225!
1269
  }
1270

1271
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
11,132,265!
1272
  param->next = NULL;
11,131,086✔
1273

1274
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
11,129,907✔
1275

1276
  stmtEnqueue(pStmt, param);
11,134,543✔
1277

1278
  return TSDB_CODE_SUCCESS;
11,132,532✔
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)) {
11,133,711!
1284
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
11,131,822✔
1285
      break;
11,132,948✔
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;
11,132,948✔
1302
}
1303

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

1308
  int64_t startUs = taosGetTimestampUs();
234,666,487✔
1309

1310
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
234,666,487✔
1311

1312
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
234,598,827!
1313
    return pStmt->errCode;
×
1314
  }
1315

1316
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
235,001,946!
1317

1318
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
235,951,813!
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) {
235,608,164!
1324
    taos_free_result(pStmt->exec.pRequest);
×
1325
    pStmt->exec.pRequest = NULL;
×
1326
  }
1327

1328
  STMT_ERR_RET(stmtCreateRequest(pStmt));
235,638,690!
1329

1330
  if (pStmt->bInfo.needParse) {
235,855,815✔
1331
    STMT_ERR_RET(stmtParseSql(pStmt));
1,073,860✔
1332
  }
1333

1334
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
235,590,808✔
1335
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
27,018!
1336

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

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

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

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

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

1368
    return TSDB_CODE_SUCCESS;
27,018✔
1369
  }
1370

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

1375
  STableDataCxt** pDataBlock = NULL;
235,198,362✔
1376

1377
  if (pStmt->exec.pCurrBlock) {
235,198,362✔
1378
    pDataBlock = &pStmt->exec.pCurrBlock;
226,110,045✔
1379
  } else {
1380
    pDataBlock =
1381
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
9,519,926!
1382
    if (NULL == pDataBlock) {
9,520,319!
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;
9,520,319✔
1387
    if (pStmt->sql.stbInterlaceMode) {
9,520,319✔
1388
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
93,225✔
1389
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
93,225✔
1390
    }
1391
  }
1392

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

1396
  SStmtQNode* param = NULL;
232,635,980✔
1397
  if (pStmt->sql.stbInterlaceMode) {
232,890,370✔
1398
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
22,263,788!
1399
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
22,263,129!
1400
    taosArrayClear(param->tblData.aCol);
11,132,948✔
1401

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

1404
    param->restoreTbCols = false;
11,128,052✔
1405
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
11,128,052!
1406
  }
1407

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

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

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

1420
  if (colIdx < 0) {
233,869,335✔
1421
    if (pStmt->sql.stbInterlaceMode) {
234,908,315✔
1422
      (*pDataBlock)->pData->flags = 0;
11,132,811✔
1423
      code =
1424
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
11,130,460✔
1425
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
11,128,139✔
1426
    } else {
1427
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
224,242,971✔
1428
                                pStmt->taos->optionInfo.charsetCxt);
223,789,868✔
1429
    }
1430

1431
    if (code) {
235,455,122✔
1432
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
9,353!
1433
      STMT_ERR_RET(code);
9,353!
1434
    }
1435
  } else {
1436
    if (pStmt->sql.stbInterlaceMode) {
6,605!
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) {
6,605!
1442
      tscError("bind column index not in sequence");
×
1443
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1444
    }
1445

1446
    pStmt->bInfo.sBindLastIdx = colIdx;
6,605✔
1447

1448
    if (0 == colIdx) {
6,605✔
1449
      pStmt->bInfo.sBindRowNum = bind->num;
3,360✔
1450
    }
1451

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

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

1464
  if (pStmt->sql.stbInterlaceMode) {
234,142,878✔
1465
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
11,133,399!
1466
  }
1467

1468
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
236,373,469✔
1469

1470
  return TSDB_CODE_SUCCESS;
236,476,774✔
1471
}
1472

1473
int stmtAddBatch(TAOS_STMT* stmt) {
228,977,162✔
1474
  STscStmt* pStmt = (STscStmt*)stmt;
228,977,162✔
1475

1476
  int64_t startUs = taosGetTimestampUs();
230,587,178✔
1477

1478
  STMT_DLOG_E("start to add batch");
230,587,178✔
1479

1480
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
231,640,895!
1481
    return pStmt->errCode;
×
1482
  }
1483

1484
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
230,322,874✔
1485

1486
  if (pStmt->sql.stbInterlaceMode) {
230,716,937✔
1487
    int64_t startUs2 = taosGetTimestampUs();
6,543,797✔
1488
    pStmt->stat.addBatchUs += startUs2 - startUs;
6,543,797✔
1489

1490
    pStmt->sql.siInfo.tableColsReady = false;
6,543,797✔
1491

1492
    SStmtQNode* param = NULL;
6,543,797✔
1493
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
13,086,157!
1494
    param->restoreTbCols = true;
6,542,360✔
1495
    param->next = NULL;
6,542,360✔
1496

1497
    stmtEnqueue(pStmt, param);
6,543,539✔
1498

1499
    return TSDB_CODE_SUCCESS;
6,543,711✔
1500
  }
1501

1502
  STMT_ERR_RET(stmtCacheBlock(pStmt));
223,819,132✔
1503

1504
  return TSDB_CODE_SUCCESS;
224,395,666✔
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) {
16,039,369✔
1641
  STscStmt*   pStmt = (STscStmt*)stmt;
16,039,369✔
1642
  int32_t     code = 0;
16,039,369✔
1643
  SSubmitRsp* pRsp = NULL;
16,039,369✔
1644

1645
  int64_t startUs = taosGetTimestampUs();
16,044,416✔
1646

1647
  STMT_DLOG_E("start to exec");
16,044,416✔
1648

1649
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
16,044,330!
1650
    return pStmt->errCode;
×
1651
  }
1652

1653
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
16,043,495!
1654

1655
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
16,044,330✔
1656
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
27,018✔
1657
  } else {
1658
    if (pStmt->sql.stbInterlaceMode) {
16,013,186✔
1659
      int64_t startTs = taosGetTimestampUs();
6,542,533✔
1660
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
17,202,933✔
1661
        taosUsleep(1);
10,660,210✔
1662
      }
1663
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
6,543,539✔
1664

1665
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
6,543,539!
1666
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
6,543,797✔
1667
      pStmt->sql.siInfo.pVgroupHash = NULL;
6,542,618✔
1668
      pStmt->sql.siInfo.pVgroupList = NULL;
6,542,618✔
1669
    } else {
1670
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
9,468,984✔
1671
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
9,468,984!
1672

1673
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
9,471,096!
1674

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

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

1681
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
16,041,248!
1682
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
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);
16,045,914✔
1693

1694
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
16,040,879✔
1695
  pStmt->affectedRows += pStmt->exec.affectedRows;
16,040,395✔
1696

1697
_return:
16,045,079✔
1698

1699
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
16,045,509✔
1700
    taosUsleep(1);
430✔
1701
  }
1702

1703
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
16,043,986!
1704

1705
  tFreeSSubmitRsp(pRsp);
16,041,432✔
1706

1707
  ++pStmt->sql.runTimes;
16,044,235✔
1708

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

1712
  STMT_RET(code);
16,045,346✔
1713
}
1714

1715
int stmtClose(TAOS_STMT* stmt) {
1,262,884✔
1716
  STscStmt* pStmt = (STscStmt*)stmt;
1,262,884✔
1717

1718
  STMT_DLOG_E("start to free stmt");
1,262,884✔
1719

1720
  if (pStmt->bindThreadInUse) {
1,262,884✔
1721
    pStmt->queue.stopQueue = true;
110,815✔
1722

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

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

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

1735
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
1,262,884!
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));
1,262,884!
1747
  taosMemoryFree(stmt);
1,262,660!
1748

1749
  return TSDB_CODE_SUCCESS;
1,263,022✔
1750
}
1751

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

1755
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
25,737!
1756
    return (char*)tstrerror(terrno);
1,498✔
1757
  }
1758

1759
  pStmt->exec.pRequest->code = terrno;
24,239✔
1760

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

1764
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
8,816✔
1765

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

1768
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
255,999,363✔
1769
  STscStmt* pStmt = (STscStmt*)stmt;
255,999,363✔
1770

1771
  STMT_DLOG_E("start is insert");
255,999,363✔
1772

1773
  if (pStmt->sql.type) {
257,527,244✔
1774
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
248,499,223!
1775
  } else {
1776
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
9,518,055✔
1777
  }
1778

1779
  return TSDB_CODE_SUCCESS;
257,670,609✔
1780
}
1781

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

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

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

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

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

1799
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
692!
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) {
692!
1805
    taos_free_result(pStmt->exec.pRequest);
×
1806
    pStmt->exec.pRequest = NULL;
×
1807
  }
1808

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

1811
  if (pStmt->bInfo.needParse) {
692!
1812
    STMT_ERRI_JRET(stmtParseSql(pStmt));
519✔
1813
  }
1814

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

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

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

1829
  return code;
692✔
1830
}
1831

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

1837
  STMT_DLOG_E("start to get col fields");
1,038!
1838

1839
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,038!
1840
    return pStmt->errCode;
×
1841
  }
1842

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

1847
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
1,038!
1848

1849
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,038!
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) {
1,038!
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));
1,038!
1861

1862
  if (pStmt->bInfo.needParse) {
1,038!
1863
    STMT_ERRI_JRET(stmtParseSql(pStmt));
865!
1864
  }
1865

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

1868
_return:
1,038✔
1869

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

1875
  pStmt->errCode = preCode;
1,038✔
1876

1877
  return code;
1,038✔
1878
}
1879

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

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

1887
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
346!
1888
    return pStmt->errCode;
×
1889
  }
1890

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

1893
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
346!
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) {
346!
1899
    taos_free_result(pStmt->exec.pRequest);
×
1900
    pStmt->exec.pRequest = NULL;
×
1901
  }
1902

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

1905
  if (pStmt->bInfo.needParse) {
346!
1906
    STMT_ERRI_JRET(stmtParseSql(pStmt));
173!
1907
  }
1908

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

1915
_return:
173✔
1916

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

1922
  pStmt->errCode = preCode;
346✔
1923

1924
  return code;
346✔
1925
}
1926

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

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

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

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

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

1944
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
692!
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) {
692!
1950
    taos_free_result(pStmt->exec.pRequest);
×
1951
    pStmt->exec.pRequest = NULL;
×
1952
  }
1953

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

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

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

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

1971
_return:
692✔
1972

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

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

1981
  return code;
692✔
1982
}
1983

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

1987
  STMT_DLOG_E("start to use result");
27,018✔
1988

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

1994
  return pStmt->exec.pRequest;
27,018✔
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