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

taosdata / TDengine / #5049

11 May 2026 06:30AM UTC coverage: 73.313% (+0.09%) from 73.222%
#5049

push

travis-ci

web-flow
feat: refactor taosdump code to improve backup speed and compression ratio (#35292)

6625 of 8435 new or added lines in 28 files covered. (78.54%)

2491 existing lines in 142 files now uncovered.

281233 of 383605 relevant lines covered (73.31%)

132489999.79 hits per line

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

81.19
/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) {
2,573,469✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
2,575,747✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
2,575,727✔
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;
2,570,981✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
2,578,005✔
44
  int i = 0;
2,578,005✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
7,201,643✔
46
    if (i < 10) {
4,625,882✔
47
      taosUsleep(1);
4,290,653✔
48
      i++;
4,288,460✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
335,229✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
335,229✔
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
335,172✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
335,127✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
2,577,621✔
58
    return false;
14,056✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
2,563,565✔
61
  SStmtQNode* node = pStmt->queue.head->next;
2,563,679✔
62
  pStmt->queue.head = pStmt->queue.head->next;
2,563,559✔
63
  *param = node;
2,563,775✔
64

65
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
2,563,718✔
66

67
  return true;
2,566,174✔
68
}
69

70
void stmtEnqueue(STscStmt* pStmt, SStmtQNode* param) {
2,561,640✔
71
  pStmt->queue.tail->next = param;
2,561,640✔
72
  pStmt->queue.tail = param;
2,563,416✔
73

74
  pStmt->stat.bindDataNum++;
2,563,371✔
75

76
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,562,396✔
77
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
2,563,204✔
78
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
2,564,794✔
79
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,561,710✔
80
}
2,560,621✔
81

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
32,616,734✔
83
  int32_t code = 0;
32,616,734✔
84

85
  if (pStmt->exec.pRequest == NULL) {
32,616,734✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
7,623,871✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
7,625,020✔
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
7,624,254✔
92
      pStmt->exec.pRequest->syncQuery = true;
7,622,722✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
7,622,722✔
94
    }
95
  }
96

97
  return code;
32,616,399✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
43,115,312✔
101
  int32_t code = 0;
43,115,312✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
43,115,312✔
104
    STMT_LOG_SEQ(newStatus);
43,138,080✔
105
  }
106

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

112
  switch (newStatus) {
43,117,818✔
113
    case STMT_PREPARE:
7,605,329✔
114
      pStmt->errCode = 0;
7,605,329✔
115
      break;
7,594,031✔
116
    case STMT_SETTBNAME:
9,792,193✔
117
      if (STMT_STATUS_EQ(INIT)) {
9,792,193✔
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
9,795,664✔
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
9,791,345✔
124
    case STMT_SETTAGS:
8,211✔
125
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
8,211✔
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
8,211✔
129
    case STMT_FETCH_FIELDS:
3,168✔
130
      if (STMT_STATUS_EQ(INIT)) {
3,168✔
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      break;
3,168✔
134
    case STMT_BIND:
9,826,339✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
9,826,339✔
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;
9,832,551✔
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:
7,942,473✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
7,942,473✔
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
7,942,032✔
154
    case STMT_EXECUTE:
7,940,105✔
155
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
7,940,105✔
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
14,156✔
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)) {
7,921,656✔
162
          code = TSDB_CODE_TSC_STMT_API_ERROR;
1,538✔
163
        }
164
      }
165
      break;
7,934,453✔
166
    default:
×
167
      code = TSDB_CODE_APP_ERROR;
×
168
      break;
×
169
  }
170

171
  STMT_ERR_RET(code);
43,105,791✔
172

173
  pStmt->sql.status = newStatus;
43,104,253✔
174

175
  return TSDB_CODE_SUCCESS;
43,117,970✔
176
}
177

178
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
7,581,861✔
179
  STscStmt* pStmt = (STscStmt*)stmt;
7,581,861✔
180

181
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
7,581,861✔
182

183
  if ('\0' == pStmt->bInfo.tbName[0]) {
7,583,384✔
184
    tscError("no table name set");
396✔
185
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
396✔
186
  }
187

188
  *tbName = pStmt->bInfo.tbName;
7,580,352✔
189

190
  return TSDB_CODE_SUCCESS;
7,582,240✔
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,
7,570,288✔
243
                           bool autoCreateTbl, uint8_t tbNameFlag) {
244
  STscStmt* pStmt = (STscStmt*)stmt;
7,570,288✔
245
  char      tbFName[TSDB_TABLE_FNAME_LEN];
7,567,134✔
246
  int32_t   code = tNameExtractFullName(tbName, tbFName);
7,575,623✔
247
  if (code != 0) {
7,575,979✔
UNCOV
248
    qDestroyBoundColInfo(tags);
×
249
    taosMemoryFreeClear(tags);
×
250
    return code;
×
251
  }
252

253
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
7,575,979✔
254
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
7,574,447✔
255
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
7,573,352✔
256

257
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
7,572,568✔
258
  pStmt->bInfo.tbSuid = pTableMeta->suid;
7,571,410✔
259
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
7,568,756✔
260
  pStmt->bInfo.tbType = pTableMeta->tableType;
7,567,287✔
261
  if (pStmt->bInfo.boundTags && !pStmt->bInfo.tagsCached) {
7,569,549✔
262
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
×
263
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
×
264
  }
265
  pStmt->bInfo.boundTags = tags;
7,570,322✔
266
  pStmt->bInfo.tagsCached = false;
7,572,566✔
267
  pStmt->bInfo.tbNameFlag = tbNameFlag;
7,575,594✔
268
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
7,570,322✔
269

270
  return TSDB_CODE_SUCCESS;
7,564,206✔
271
}
272

273
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
7,567,956✔
274
  STscStmt* pStmt = (STscStmt*)stmt;
7,567,956✔
275

276
  pStmt->sql.pVgHash = pVgHash;
7,567,956✔
277
  pStmt->exec.pBlockHash = pBlockHash;
7,576,759✔
278

279
  return TSDB_CODE_SUCCESS;
7,564,599✔
280
}
281

282
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SSHashObj** cols, SName* tbName,
7,575,605✔
283
                       bool autoCreateTbl, SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName,
284
                       uint8_t tbNameFlag) {
285
  STscStmt* pStmt = (STscStmt*)stmt;
7,575,605✔
286

287
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
7,575,605✔
288
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
7,561,160✔
289

290
  pStmt->sql.autoCreateTbl = autoCreateTbl;
7,564,225✔
291
  if (pStmt->sql.autoCreateTbl) {
7,562,367✔
292
    pStmt->sql.stbInterlaceMode = false;
7,556,414✔
293
  }
294

295
  return TSDB_CODE_SUCCESS;
7,557,445✔
296
}
297

298
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
299
  STscStmt* pStmt = (STscStmt*)stmt;
×
300

301
  *pVgHash = pStmt->sql.pVgHash;
×
302
  pStmt->sql.pVgHash = NULL;
×
303

304
  *pBlockHash = pStmt->exec.pBlockHash;
×
305
  pStmt->exec.pBlockHash = NULL;
×
306

307
  return TSDB_CODE_SUCCESS;
×
308
}
309

310
int32_t stmtCacheBlock(STscStmt* pStmt) {
7,600,586✔
311
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
7,600,586✔
312
    return TSDB_CODE_SUCCESS;
18,288✔
313
  }
314

315
  uint64_t uid = pStmt->bInfo.tbUid;
7,580,793✔
316
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
7,581,951✔
317

318
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
7,584,177✔
319
    return TSDB_CODE_SUCCESS;
12,940✔
320
  }
321

322
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
7,575,788✔
323
  if (!pSrc) {
7,572,003✔
324
    return terrno;
×
325
  }
326
  STableDataCxt* pDst = NULL;
7,572,003✔
327

328
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
7,573,134✔
329

330
  SStmtTableCache cache = {
7,565,956✔
331
      .pDataCtx = pDst,
332
      .boundTags = pStmt->bInfo.boundTags,
7,565,582✔
333
  };
334

335
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
7,570,471✔
336
    return terrno;
×
337
  }
338

339
  if (pStmt->sql.autoCreateTbl) {
7,574,256✔
340
    pStmt->bInfo.tagsCached = true;
7,572,726✔
341
  } else {
342
    pStmt->bInfo.boundTags = NULL;
385✔
343
  }
344

345
  return TSDB_CODE_SUCCESS;
7,571,237✔
346
}
347

348
int32_t stmtParseSql(STscStmt* pStmt) {
7,600,830✔
349
  pStmt->exec.pCurrBlock = NULL;
7,600,830✔
350

351
  SStmtCallback stmtCb = {
7,603,167✔
352
      .pStmt = pStmt,
353
      .getTbNameFn = stmtGetTbName,
354
      .setInfoFn = stmtUpdateInfo,
355
      .getExecInfoFn = stmtGetExecInfo,
356
  };
357

358
  STMT_ERR_RET(stmtCreateRequest(pStmt));
7,603,176✔
359
  pStmt->exec.pRequest->stmtBindVersion = 1;
7,598,653✔
360

361
  pStmt->stat.parseSqlNum++;
7,600,149✔
362
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
7,600,167✔
363
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
7,587,101✔
364

365
  pStmt->bInfo.needParse = false;
7,587,858✔
366

367
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
7,593,959✔
368
    pStmt->sql.type = STMT_TYPE_INSERT;
3,764✔
369
    pStmt->sql.stbInterlaceMode = false;
3,764✔
370
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
7,582,981✔
371
    pStmt->sql.type = STMT_TYPE_QUERY;
13,718✔
372
    pStmt->sql.stbInterlaceMode = false;
13,718✔
373

374
    return TSDB_CODE_SUCCESS;
13,718✔
375
  }
376

377
  STableDataCxt** pSrc =
378
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
7,576,046✔
379
  if (NULL == pSrc || NULL == *pSrc) {
7,586,680✔
380
    return terrno;
1,550✔
381
  }
382

383
  STableDataCxt* pTableCtx = *pSrc;
7,577,533✔
384
  if (pStmt->sql.stbInterlaceMode) {
7,573,392✔
385
    int16_t lastIdx = -1;
6,358✔
386

387
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
47,566✔
388
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
41,208✔
389
        pStmt->sql.stbInterlaceMode = false;
×
390
        break;
×
391
      }
392

393
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
41,208✔
394
    }
395
  }
396

397
  if (NULL == pStmt->sql.pBindInfo) {
7,573,374✔
398
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
7,576,803✔
399
    if (NULL == pStmt->sql.pBindInfo) {
7,579,466✔
400
      return terrno;
×
401
    }
402
  }
403

404
  return TSDB_CODE_SUCCESS;
7,568,494✔
405
}
406

407
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
15,555,686✔
408
  pStmt->bInfo.tbUid = 0;
15,555,686✔
409
  pStmt->bInfo.tbSuid = 0;
15,557,334✔
410
  pStmt->bInfo.tbVgId = -1;
15,558,397✔
411
  pStmt->bInfo.tbType = 0;
15,553,927✔
412
  pStmt->bInfo.needParse = true;
15,555,577✔
413
  pStmt->bInfo.inExecCache = false;
15,554,361✔
414

415
  pStmt->bInfo.tbName[0] = 0;
15,555,670✔
416
  pStmt->bInfo.tbFName[0] = 0;
15,556,426✔
417
  if (!pStmt->bInfo.tagsCached) {
15,556,044✔
418
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
405,323✔
419
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
405,388✔
420
  }
421
  pStmt->bInfo.stbFName[0] = 0;
15,554,141✔
422

423
  return TSDB_CODE_SUCCESS;
15,554,218✔
424
}
425

426
void stmtFreeTableBlkList(STableColsData* pTb) {
×
427
  (void)qResetStmtColumns(pTb->aCol, true);
×
428
  taosArrayDestroy(pTb->aCol);
×
429
}
×
430

431
void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
342,073✔
432
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
342,073✔
433
  if (NULL == pTblBuf->pCurBuff) {
342,283✔
434
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
435
    return;
×
436
  }
437
  pTblBuf->buffIdx = 1;
342,283✔
438
  pTblBuf->buffOffset = sizeof(*pQueue->head);
342,283✔
439

440
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
342,058✔
441
  pQueue->qRemainNum = 0;
342,168✔
442
  pQueue->head->next = NULL;
342,111✔
443
}
444

445
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
15,553,510✔
446
  if (pStmt->sql.stbInterlaceMode) {
15,553,510✔
447
    if (deepClean) {
348,429✔
448
      taosHashCleanup(pStmt->exec.pBlockHash);
6,358✔
449
      pStmt->exec.pBlockHash = NULL;
6,358✔
450

451
      if (NULL != pStmt->exec.pCurrBlock) {
6,358✔
452
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
6,358✔
453
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
6,358✔
454
      }
455
    } else {
456
      pStmt->sql.siInfo.pTableColsIdx = 0;
342,071✔
457
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
342,291✔
458
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
342,126✔
459
    }
460
  } else {
461
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
15,205,523✔
462
      taos_free_result(pStmt->exec.pRequest);
15,193,618✔
463
      pStmt->exec.pRequest = NULL;
15,189,067✔
464
    }
465

466
    size_t keyLen = 0;
15,205,476✔
467
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
15,205,476✔
468
    while (pIter) {
30,384,367✔
469
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
15,175,070✔
470
      char*          key = taosHashGetKey(pIter, &keyLen);
15,175,435✔
471
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
15,175,052✔
472

473
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
15,176,584✔
474
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
7,591,690✔
475
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
7,594,524✔
476

477
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
7,584,868✔
478
        continue;
7,592,438✔
479
      }
480

481
      qDestroyStmtDataBlock(pBlocks);
7,584,894✔
482
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
7,581,091✔
483

484
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
7,581,073✔
485
    }
486

487
    if (keepTable) {
15,209,297✔
488
      return TSDB_CODE_SUCCESS;
7,606,594✔
489
    }
490

491
    taosHashCleanup(pStmt->exec.pBlockHash);
7,602,703✔
492
    pStmt->exec.pBlockHash = NULL;
7,602,329✔
493

494
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
7,602,329✔
495
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
7,600,405✔
496
  }
497

498
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
7,950,183✔
499

500
  return TSDB_CODE_SUCCESS;
7,946,250✔
501
}
502

503
void stmtFreeTbBuf(void* buf) {
14,056✔
504
  void* pBuf = *(void**)buf;
14,056✔
505
  taosMemoryFree(pBuf);
14,056✔
506
}
14,056✔
507

508
void stmtFreeTbCols(void* buf) {
6,358,000✔
509
  SArray* pCols = *(SArray**)buf;
6,358,000✔
510
  taosArrayDestroy(pCols);
6,358,000✔
511
}
6,358,000✔
512

513
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
7,604,596✔
514
  STMT_DLOG_E("start to free SQL info");
7,604,596✔
515

516
  taosMemoryFree(pStmt->sql.pBindInfo);
7,604,596✔
517
  taosMemoryFree(pStmt->sql.queryRes.fields);
7,605,763✔
518
  taosMemoryFree(pStmt->sql.queryRes.userFields);
7,605,362✔
519
  taosMemoryFree(pStmt->sql.sqlStr);
7,604,979✔
520
  qDestroyQuery(pStmt->sql.pQuery);
7,601,942✔
521
  taosArrayDestroy(pStmt->sql.nodeList);
7,607,259✔
522
  taosHashCleanup(pStmt->sql.pVgHash);
7,607,259✔
523
  pStmt->sql.pVgHash = NULL;
7,606,876✔
524

525
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
7,607,633✔
526
  while (pIter) {
15,184,926✔
527
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
7,576,910✔
528

529
    qDestroyStmtDataBlock(pCache->pDataCtx);
7,576,910✔
530
    qDestroyBoundColInfo(pCache->boundTags);
7,572,724✔
531
    taosMemoryFreeClear(pCache->boundTags);
7,575,770✔
532

533
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
7,575,396✔
534
  }
535
  taosHashCleanup(pStmt->sql.pTableCache);
7,608,016✔
536
  pStmt->sql.pTableCache = NULL;
7,607,633✔
537

538
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
7,607,259✔
539
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
7,605,362✔
540

541
  taos_free_result(pStmt->sql.siInfo.pRequest);
7,606,128✔
542
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
7,607,259✔
543
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
7,606,885✔
544
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
7,608,025✔
545
  tSimpleHashCleanup(pStmt->sql.predicateCols);
7,607,259✔
546
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
7,607,268✔
547
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
7,606,110✔
548
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
7,606,493✔
549
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
7,606,110✔
550

551
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
7,607,259✔
552
  pStmt->sql.siInfo.tableColsReady = true;
7,607,642✔
553

554
  STMT_DLOG_E("end to free SQL info");
7,602,690✔
555

556
  return TSDB_CODE_SUCCESS;
7,603,848✔
557
}
558

559
int32_t stmtTryAddTableVgroupInfo(STscStmt* pStmt, int32_t* vgId) {
3,824✔
560
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
3,824✔
561
    return TSDB_CODE_SUCCESS;
1,677✔
562
  }
563

564
  SVgroupInfo      vgInfo = {0};
2,147✔
565
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2,147✔
566
                           .requestId = pStmt->exec.pRequest->requestId,
2,147✔
567
                           .requestObjRefId = pStmt->exec.pRequest->self,
2,147✔
568
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2,147✔
569

570
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
2,147✔
571
  if (TSDB_CODE_SUCCESS != code) {
2,147✔
572
    return code;
×
573
  }
574

575
  code =
576
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
2,147✔
577
  if (TSDB_CODE_SUCCESS != code) {
2,147✔
578
    return code;
×
579
  }
580

581
  *vgId = vgInfo.vgId;
2,147✔
582

583
  return TSDB_CODE_SUCCESS;
2,147✔
584
}
585

586
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
3,824✔
587
                             uint64_t suid, int32_t vgId) {
588
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
3,824✔
589
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
3,824✔
590

591
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
3,824✔
592

593
  return TSDB_CODE_SUCCESS;
3,824✔
594
}
595

596
int32_t stmtGetTableMeta(STscStmt* pStmt, STableMeta** ppTableMeta) {
2,178✔
597
  if (NULL == pStmt->pCatalog) {
2,178✔
598
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
1,584✔
599
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
1,584✔
600
  }
601

602
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2,178✔
603
                           .requestId = pStmt->exec.pRequest->requestId,
2,178✔
604
                           .requestObjRefId = pStmt->exec.pRequest->self,
2,178✔
605
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2,178✔
606

607
  int32_t code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, ppTableMeta);
2,178✔
608
  if (TSDB_CODE_SUCCESS != code) {
2,178✔
609
    STMT_ELOG("get table meta failed, code: 0x%x", code);
198✔
610
    return code;
198✔
611
  }
612

613
  return TSDB_CODE_SUCCESS;
1,980✔
614
}
615

616
int32_t stmtGetFromCache(STscStmt* pStmt) {
7,584,682✔
617
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
7,584,682✔
618
    pStmt->bInfo.needParse = false;
×
619
    pStmt->bInfo.inExecCache = false;
×
620
    return TSDB_CODE_SUCCESS;
×
621
  }
622

623
  pStmt->bInfo.needParse = true;
7,584,673✔
624
  pStmt->bInfo.inExecCache = false;
7,583,159✔
625

626
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
7,580,532✔
627
  if (pCxtInExec) {
7,587,412✔
628
    pStmt->bInfo.needParse = false;
208✔
629
    pStmt->bInfo.inExecCache = true;
208✔
630

631
    pStmt->exec.pCurrBlock = *pCxtInExec;
208✔
632

633
    if (pStmt->sql.autoCreateTbl) {
208✔
634
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
208✔
635
      return TSDB_CODE_SUCCESS;
208✔
636
    }
637
  }
638

639
  if (NULL == pStmt->pCatalog) {
7,587,204✔
640
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
21,527✔
641
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
21,525✔
642
  }
643

644
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
7,586,388✔
645
    if (pStmt->bInfo.inExecCache) {
7,582,623✔
646
      pStmt->bInfo.needParse = false;
×
647
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
648
      return TSDB_CODE_SUCCESS;
×
649
    }
650

651
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
7,581,875✔
652
    return TSDB_CODE_SUCCESS;
7,575,725✔
653
  }
654

655
  if (pStmt->sql.autoCreateTbl) {
3,824✔
656
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
1,935✔
657
    if (pCache) {
1,935✔
658
      pStmt->bInfo.needParse = false;
1,935✔
659
      pStmt->bInfo.tbUid = 0;
1,935✔
660

661
      STableDataCxt* pNewBlock = NULL;
1,935✔
662
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
1,935✔
663

664
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
1,935✔
665
                      POINTER_BYTES)) {
666
        STMT_ERR_RET(terrno);
×
667
      }
668

669
      pStmt->exec.pCurrBlock = pNewBlock;
1,935✔
670

671
      tscDebug("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
1,935✔
672

673
      return TSDB_CODE_SUCCESS;
1,935✔
674
    }
675

676
    STMT_RET(stmtCleanBindInfo(pStmt));
×
677
  }
678

679
  uint64_t uid, suid;
680
  int32_t  vgId;
681
  int8_t   tableType;
682

683
  STableMeta*      pTableMeta = NULL;
1,889✔
684
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1,889✔
685
                           .requestId = pStmt->exec.pRequest->requestId,
1,889✔
686
                           .requestObjRefId = pStmt->exec.pRequest->self,
1,889✔
687
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1,889✔
688
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1,889✔
689

690
  pStmt->stat.ctgGetTbMetaNum++;
1,889✔
691

692
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
1,889✔
693
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
694
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
695

696
    STMT_ERR_RET(code);
×
697
  }
698

699
  STMT_ERR_RET(code);
1,889✔
700

701
  uid = pTableMeta->uid;
1,889✔
702
  suid = pTableMeta->suid;
1,889✔
703
  tableType = pTableMeta->tableType;
1,889✔
704
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
1,889✔
705
  vgId = pTableMeta->vgId;
1,889✔
706

707
  taosMemoryFree(pTableMeta);
1,889✔
708

709
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
1,889✔
710

711
  if (uid == pStmt->bInfo.tbUid) {
1,889✔
712
    pStmt->bInfo.needParse = false;
×
713

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

716
    return TSDB_CODE_SUCCESS;
×
717
  }
718

719
  if (pStmt->bInfo.inExecCache) {
1,889✔
720
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
721
    if (NULL == pCache) {
×
722
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
723
               pStmt->bInfo.tbFName, uid, cacheUid);
724

725
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
726
    }
727

728
    pStmt->bInfo.needParse = false;
×
729

730
    pStmt->bInfo.tbUid = uid;
×
731
    pStmt->bInfo.tbSuid = suid;
×
732
    pStmt->bInfo.tbType = tableType;
×
733
    pStmt->bInfo.boundTags = pCache->boundTags;
×
734
    pStmt->bInfo.tagsCached = true;
×
735

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

738
    return TSDB_CODE_SUCCESS;
×
739
  }
740

741
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
1,889✔
742
  if (pCache) {
1,889✔
743
    pStmt->bInfo.needParse = false;
1,889✔
744

745
    pStmt->bInfo.tbUid = uid;
1,889✔
746
    pStmt->bInfo.tbSuid = suid;
1,889✔
747
    pStmt->bInfo.tbType = tableType;
1,889✔
748
    pStmt->bInfo.boundTags = pCache->boundTags;
1,889✔
749
    pStmt->bInfo.tagsCached = true;
1,889✔
750

751
    STableDataCxt* pNewBlock = NULL;
1,889✔
752
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
1,889✔
753

754
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
1,889✔
755
                    POINTER_BYTES)) {
756
      STMT_ERR_RET(terrno);
×
757
    }
758

759
    pStmt->exec.pCurrBlock = pNewBlock;
1,889✔
760

761
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
1,889✔
762

763
    return TSDB_CODE_SUCCESS;
1,889✔
764
  }
765

766
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
767

768
  return TSDB_CODE_SUCCESS;
×
769
}
770

771
int32_t stmtResetStmt(STscStmt* pStmt) {
7,559,906✔
772
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
7,559,906✔
773

774
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
7,559,915✔
775
  if (NULL == pStmt->sql.pTableCache) {
7,560,280✔
776
    STMT_ERR_RET(terrno);
×
777
  }
778

779
  if (pStmt->sql.siInfo.pTableRowDataHash) {
7,560,672✔
780
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
781
  }
782

783
  pStmt->sql.status = STMT_INIT;
7,559,906✔
784

785
  return TSDB_CODE_SUCCESS;
7,561,037✔
786
}
787

788
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
2,564,221✔
789
  SStmtQNode* pParam = (SStmtQNode*)param;
2,564,221✔
790

791
  if (pParam->restoreTbCols) {
2,564,221✔
792
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
2,564,863✔
793
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
2,222,560✔
794
      *p = taosArrayInit(20, POINTER_BYTES);
2,222,725✔
795
      if (*p == NULL) {
2,222,857✔
796
        STMT_ERR_RET(terrno);
×
797
      }
798
    }
799

800
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
342,303✔
801
  } else {
802
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
2,222,344✔
803
                                        &pStmt->sql.siInfo));
804

805
    // taosMemoryFree(pParam->pTbData);
806

807
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
2,219,440✔
808
  }
809
  return TSDB_CODE_SUCCESS;
2,565,661✔
810
}
811

812
void* stmtBindThreadFunc(void* param) {
14,056✔
813
  setThreadName("stmtBind");
14,056✔
814

815
  qInfo("stmt bind thread started");
14,056✔
816

817
  STscStmt* pStmt = (STscStmt*)param;
14,056✔
818

819
  while (true) {
2,579,039✔
820
    if (pStmt->queue.stopQueue) {
2,593,095✔
821
      break;
14,056✔
822
    }
823

824
    SStmtQNode* asyncParam = NULL;
2,579,025✔
825
    if (!stmtDequeue(pStmt, &asyncParam)) {
2,578,911✔
826
      continue;
14,056✔
827
    }
828

829
    int ret = stmtAsyncOutput(pStmt, asyncParam);
2,565,376✔
830
    if (ret != 0) {
2,564,698✔
831
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
832
    }
833
  }
834

835
  qInfo("stmt bind thread stopped");
14,227✔
836

837
  return NULL;
14,056✔
838
}
839

840
int32_t stmtStartBindThread(STscStmt* pStmt) {
14,056✔
841
  TdThreadAttr thAttr;
13,435✔
842
  if (taosThreadAttrInit(&thAttr) != 0) {
14,056✔
843
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
844
  }
845
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
14,056✔
846
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
847
  }
848

849
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
14,056✔
850
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
851
    STMT_ERR_RET(terrno);
×
852
  }
853

854
  pStmt->bindThreadInUse = true;
14,056✔
855

856
  (void)taosThreadAttrDestroy(&thAttr);
14,056✔
857
  return TSDB_CODE_SUCCESS;
14,056✔
858
}
859

860
int32_t stmtInitQueue(STscStmt* pStmt) {
14,056✔
861
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
14,056✔
862
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
14,056✔
863
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
28,112✔
864
  pStmt->queue.tail = pStmt->queue.head;
14,056✔
865

866
  return TSDB_CODE_SUCCESS;
14,056✔
867
}
868

869
int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
14,056✔
870
  pTblBuf->buffUnit = sizeof(SStmtQNode);
14,056✔
871
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
14,056✔
872
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
14,056✔
873
  if (NULL == pTblBuf->pBufList) {
14,056✔
874
    return terrno;
×
875
  }
876
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
14,056✔
877
  if (NULL == buff) {
14,056✔
878
    return terrno;
×
879
  }
880

881
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
28,112✔
882
    return terrno;
×
883
  }
884

885
  pTblBuf->pCurBuff = buff;
14,056✔
886
  pTblBuf->buffIdx = 1;
14,056✔
887
  pTblBuf->buffOffset = 0;
14,056✔
888

889
  return TSDB_CODE_SUCCESS;
14,056✔
890
}
891

892
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid, TAOS_STMT_OPTIONS* pOptions) {
44,905✔
893
  STscObj*  pObj = (STscObj*)taos;
44,905✔
894
  STscStmt* pStmt = NULL;
44,905✔
895
  int32_t   code = 0;
44,905✔
896

897
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
44,905✔
898
  if (NULL == pStmt) {
45,073✔
899
    return NULL;
×
900
  }
901

902
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
45,073✔
903
  if (NULL == pStmt->sql.pTableCache) {
45,073✔
904
    taosMemoryFree(pStmt);
×
905
    return NULL;
×
906
  }
907

908
  pStmt->taos = pObj;
45,073✔
909
  pStmt->bInfo.needParse = true;
45,073✔
910
  pStmt->sql.status = STMT_INIT;
45,073✔
911
  pStmt->reqid = reqid;
45,073✔
912
  pStmt->errCode = TSDB_CODE_SUCCESS;
45,073✔
913

914
  if (NULL != pOptions) {
45,073✔
915
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
14,056✔
916
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
14,056✔
917
      pStmt->stbInterlaceMode = true;
14,056✔
918
    }
919
  }
920

921
  if (pStmt->stbInterlaceMode) {
45,073✔
922
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
14,056✔
923
    pStmt->sql.siInfo.acctId = taos->acctId;
14,056✔
924
    pStmt->sql.siInfo.dbname = taos->db;
14,056✔
925
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
14,056✔
926
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
14,056✔
927
    if (NULL == pStmt->sql.siInfo.pTableHash) {
14,056✔
928
      (void)stmtClose(pStmt);
×
929
      return NULL;
×
930
    }
931
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
14,056✔
932
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
14,056✔
933
      STMT_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
934
      (void)stmtClose(pStmt);
×
935
      return NULL;
×
936
    }
937
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
14,056✔
938
    if (NULL == pStmt->sql.siInfo.pTableCols) {
14,056✔
939
      (void)stmtClose(pStmt);
×
940
      return NULL;
×
941
    }
942

943
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
14,056✔
944
    if (TSDB_CODE_SUCCESS == code) {
14,056✔
945
      code = stmtInitQueue(pStmt);
14,056✔
946
    }
947
    if (TSDB_CODE_SUCCESS == code) {
14,056✔
948
      code = stmtStartBindThread(pStmt);
14,056✔
949
    }
950
    if (TSDB_CODE_SUCCESS != code) {
14,056✔
951
      terrno = code;
×
952
      (void)stmtClose(pStmt);
×
953
      return NULL;
×
954
    }
955
  }
956

957
  pStmt->sql.siInfo.tableColsReady = true;
45,073✔
958

959
  STMT_LOG_SEQ(STMT_INIT);
45,073✔
960

961
  tscDebug("stmt:%p initialized", pStmt);
45,073✔
962

963
  return pStmt;
45,073✔
964
}
965

966
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
7,605,338✔
967
  STscStmt* pStmt = (STscStmt*)stmt;
7,605,338✔
968

969
  STMT_DLOG_E("start to prepare");
7,605,338✔
970

971
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,605,338✔
972
    return pStmt->errCode;
×
973
  }
974

975
  if (pStmt->sql.status >= STMT_PREPARE) {
7,607,235✔
976
    STMT_ERR_RET(stmtResetStmt(pStmt));
7,561,438✔
977
  }
978

979
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
7,605,685✔
980

981
  if (length <= 0) {
7,603,815✔
982
    length = strlen(sql);
5,013✔
983
  }
984

985
  pStmt->sql.sqlStr = taosStrndup(sql, length);
7,603,815✔
986
  if (!pStmt->sql.sqlStr) {
7,607,226✔
987
    return terrno;
×
988
  }
989
  pStmt->sql.sqlLen = length;
7,604,171✔
990
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
7,606,086✔
991

992
  STMT_ERR_RET(stmtCreateRequest(pStmt));
7,607,235✔
993

994
  int32_t code = 0;
7,602,292✔
995
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
15,203,428✔
996
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
15,203,811✔
997
    // get table meta
998
    STableMeta* pTableMeta = NULL;
2,178✔
999
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
2,970✔
1000

1001
    char* newSql = NULL;
1,980✔
1002

1003
    // conver update sql to insert sql
1004
    pStmt->sql.predicateCols = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
1,980✔
1005
    if (NULL == pStmt->sql.predicateCols) {
1,980✔
1006
      STMT_ELOG("fail to allocate memory for predicateCols:%s", tstrerror(terrno));
×
1007
      return terrno;
×
1008
    }
1009

1010
    code = convertUpdateToInsert(sql, &newSql, pTableMeta, pStmt->sql.predicateCols, pStmt->exec.pRequest->msgBuf,
3,960✔
1011
                                 pStmt->exec.pRequest->msgBufLen);
1,980✔
1012
    taosMemoryFree(pTableMeta);
1,980✔
1013

1014
    if (TSDB_CODE_SUCCESS != code) {
1,980✔
1015
      STMT_ELOG("convert update sql to insert sql failed, code: 0x%x", code);
792✔
1016
      return code;
792✔
1017
    }
1018

1019
    // reset request sql
1020
    if (pStmt->exec.pRequest->sqlstr) {
1,188✔
1021
      taosMemoryFreeClear(pStmt->exec.pRequest->sqlstr);
1,188✔
1022
    }
1023
    pStmt->exec.pRequest->sqlstr = newSql;
1,188✔
1024
    pStmt->exec.pRequest->sqlLen = strlen(newSql);
1,188✔
1025

1026
    if (pStmt->sql.sqlStr) {
1,188✔
1027
      taosMemoryFreeClear(pStmt->sql.sqlStr);
1,188✔
1028
    }
1029
    pStmt->sql.sqlStr = taosStrndup(newSql, strlen(newSql));
1,188✔
1030
    pStmt->sql.sqlLen = strlen(newSql);
1,188✔
1031
  }
1032

1033
  STMT_ERR_RET(code);
7,601,685✔
1034

1035
  char* dbName = NULL;
7,601,487✔
1036
  if (qParseDbName(sql, length, &dbName)) {
7,603,767✔
1037
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
988✔
1038
    taosMemoryFreeClear(dbName);
803✔
1039
  }
1040

1041
  return TSDB_CODE_SUCCESS;
7,599,225✔
1042
}
1043

1044
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
6,338✔
1045
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
6,338✔
1046
  if (!pSrc) {
6,358✔
1047
    return terrno;
×
1048
  }
1049
  STableDataCxt* pDst = NULL;
6,358✔
1050

1051
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
6,358✔
1052
  pStmt->sql.siInfo.pDataCtx = pDst;
6,307✔
1053

1054
  SArray* pTblCols = NULL;
6,307✔
1055
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
6,327,621✔
1056
    pTblCols = taosArrayInit(20, POINTER_BYTES);
6,321,321✔
1057
    if (NULL == pTblCols) {
6,318,992✔
1058
      return terrno;
×
1059
    }
1060

1061
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
12,642,336✔
1062
      return terrno;
×
1063
    }
1064
  }
1065

1066
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
6,300✔
1067

1068
  return TSDB_CODE_SUCCESS;
6,300✔
1069
}
1070

1071
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
988✔
1072
  STscStmt* pStmt = (STscStmt*)stmt;
988✔
1073

1074
  STMT_DLOG("start to set dbName:%s", dbName);
988✔
1075

1076
  STMT_ERR_RET(stmtCreateRequest(pStmt));
988✔
1077

1078
  // The SQL statement specifies a database name, overriding the previously specified database
1079
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
988✔
1080
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
988✔
1081
  if (pStmt->exec.pRequest->pDb == NULL) {
988✔
1082
    return terrno;
×
1083
  }
1084
  return TSDB_CODE_SUCCESS;
988✔
1085
}
1086

1087
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
9,789,328✔
1088
  STscStmt* pStmt = (STscStmt*)stmt;
9,789,328✔
1089

1090
  int64_t startUs = taosGetTimestampUs();
9,798,825✔
1091

1092
  STMT_DLOG("start to set tbName:%s", tbName);
9,798,825✔
1093

1094
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9,798,837✔
1095
    return pStmt->errCode;
×
1096
  }
1097

1098
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
9,797,766✔
1099

1100
  int32_t insert = 0;
9,794,616✔
1101
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
9,793,208✔
1102
  if (0 == insert) {
9,793,551✔
1103
    tscError("set tb name not available for none insert statement");
×
1104
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1105
  }
1106

1107
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
9,793,551✔
1108
    STMT_ERR_RET(stmtCreateRequest(pStmt));
7,582,351✔
1109

1110
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
7,586,262✔
1111
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1112
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
7,583,159✔
1113

1114
    STMT_ERR_RET(stmtGetFromCache(pStmt));
7,583,548✔
1115

1116
    if (pStmt->bInfo.needParse) {
7,583,533✔
1117
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
7,579,501✔
1118
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
7,577,239✔
1119

1120
      STMT_ERR_RET(stmtParseSql(pStmt));
7,578,370✔
1121
    }
1122
  } else {
1123
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
2,206,934✔
1124
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
2,206,765✔
1125
    pStmt->exec.pRequest->requestId++;
2,207,803✔
1126
    pStmt->bInfo.needParse = false;
2,212,268✔
1127
  }
1128

1129
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
9,785,329✔
1130
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
6,358✔
1131
  }
1132

1133
  int64_t startUs2 = taosGetTimestampUs();
9,780,813✔
1134
  pStmt->stat.setTbNameUs += startUs2 - startUs;
9,780,813✔
1135

1136
  return TSDB_CODE_SUCCESS;
9,781,063✔
1137
}
1138

1139
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
8,211✔
1140
  STscStmt* pStmt = (STscStmt*)stmt;
8,211✔
1141

1142
  STMT_DLOG_E("start to set tbTags");
8,211✔
1143

1144
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
8,211✔
1145
    return pStmt->errCode;
×
1146
  }
1147

1148
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
8,211✔
1149

1150
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
8,211✔
1151
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
8,211✔
1152
    tscWarn("no tags bound in sql, will not bound tags");
×
1153
    return TSDB_CODE_SUCCESS;
×
1154
  }
1155

1156
  if (pStmt->bInfo.inExecCache) {
8,211✔
1157
    return TSDB_CODE_SUCCESS;
×
1158
  }
1159

1160
  STableDataCxt** pDataBlock =
1161
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
8,211✔
1162
  if (NULL == pDataBlock) {
8,211✔
1163
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1164
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1165
  }
1166

1167
  tscDebug("start to bind stmt tag values");
8,211✔
1168
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
8,211✔
1169
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1170
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1171

1172
  return TSDB_CODE_SUCCESS;
8,211✔
1173
}
1174

1175
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
396✔
1176
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
396✔
1177
    return pStmt->errCode;
198✔
1178
  }
1179

1180
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
198✔
1181
    tscError("invalid operation to get query tag fileds");
×
1182
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1183
  }
1184

1185
  STableDataCxt** pDataBlock =
1186
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
198✔
1187
  if (NULL == pDataBlock) {
198✔
1188
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1189
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1190
  }
1191

1192
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
198✔
1193

1194
  return TSDB_CODE_SUCCESS;
198✔
1195
}
1196

1197
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2,178✔
1198
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,178✔
1199
    return pStmt->errCode;
×
1200
  }
1201

1202
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,178✔
1203
    tscError("invalid operation to get query column fileds");
×
1204
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1205
  }
1206

1207
  STableDataCxt** pDataBlock = NULL;
2,178✔
1208

1209
  if (pStmt->sql.stbInterlaceMode) {
2,178✔
1210
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1211
  } else {
1212
    pDataBlock =
1213
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,178✔
1214
    if (NULL == pDataBlock) {
2,178✔
1215
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1216
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1217
    }
1218
  }
1219
  if (pStmt->sql.predicateCols) {
2,178✔
1220
    STMT_ERR_RET(qBuildUpdateStmtColFields(*pDataBlock, fieldNum, fields, pStmt->sql.predicateCols));
990✔
1221
  } else {
1222
    STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
1,188✔
1223
  }
1224

1225
  return TSDB_CODE_SUCCESS;
2,178✔
1226
}
1227

1228
/*
1229
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1230
  while (true) {
1231
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1232
      pStmt->exec.smInfo.pColIdx = 0;
1233
    }
1234

1235
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1236
      taosUsleep(1);
1237
      continue;
1238
    }
1239

1240
    *idx = pStmt->exec.smInfo.pColIdx;
1241
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1242
  }
1243
}
1244
*/
1245

1246
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
2,218,105✔
1247
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
2,218,105✔
1248
    pStmt->sql.siInfo.pVgroupHash =
342,138✔
1249
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
342,132✔
1250
  }
1251
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
2,220,304✔
1252
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
342,297✔
1253
  }
1254

1255
  if (NULL == pStmt->sql.siInfo.pRequest) {
2,220,068✔
1256
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
6,358✔
1257
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1258

1259
    if (pStmt->reqid != 0) {
6,358✔
1260
      pStmt->reqid++;
×
1261
    }
1262
    pStmt->exec.pRequest->syncQuery = true;
6,358✔
1263

1264
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
6,358✔
1265
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
6,358✔
1266
  }
1267

1268
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
2,220,689✔
1269
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
36,476✔
1270
    pStmt->sql.siInfo.tbFromHash = true;
1,499✔
1271
  }
1272

1273
  if (0 == pStmt->sql.siInfo.firstName[0]) {
2,216,945✔
1274
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
6,358✔
1275
  }
1276

1277
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
2,218,690✔
1278
  param->next = NULL;
2,219,215✔
1279

1280
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
2,218,126✔
1281

1282
  stmtEnqueue(pStmt, param);
2,222,497✔
1283

1284
  return TSDB_CODE_SUCCESS;
2,218,216✔
1285
}
1286

1287
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1288
  while (true) {
1289
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
2,218,502✔
1290
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
2,215,052✔
1291
      break;
2,213,428✔
1292
    } else {
1293
      SArray* pTblCols = NULL;
×
1294
      for (int32_t i = 0; i < 100; i++) {
×
1295
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1296
        if (NULL == pTblCols) {
×
1297
          return terrno;
×
1298
        }
1299

1300
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1301
          return terrno;
×
1302
        }
1303
      }
1304
    }
1305
  }
1306

1307
  return TSDB_CODE_SUCCESS;
2,213,428✔
1308
}
1309

1310
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
9,832,438✔
1311
  STscStmt* pStmt = (STscStmt*)stmt;
9,832,438✔
1312
  int32_t   code = 0;
9,832,438✔
1313

1314
  int64_t startUs = taosGetTimestampUs();
9,828,642✔
1315

1316
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
9,828,642✔
1317

1318
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9,828,642✔
1319
    return pStmt->errCode;
×
1320
  }
1321

1322
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
9,829,315✔
1323

1324
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9,830,465✔
1325
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1326
    pStmt->bInfo.needParse = false;
×
1327
  }
1328

1329
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
9,828,964✔
1330
    taos_free_result(pStmt->exec.pRequest);
438✔
1331
    pStmt->exec.pRequest = NULL;
438✔
1332
  }
1333

1334
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,823,795✔
1335

1336
  if (pStmt->bInfo.needParse) {
9,824,886✔
1337
    STMT_ERR_RET(stmtParseSql(pStmt));
18,407✔
1338
  }
1339

1340
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
9,818,982✔
1341
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
14,156✔
1342

1343
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
14,156✔
1344
                         .acctId = pStmt->taos->acctId,
14,156✔
1345
                         .minSecLevel = pStmt->taos->minSecLevel,
14,156✔
1346
                         .maxSecLevel = pStmt->taos->maxSecLevel,
14,156✔
1347
                         .sodInitial = pStmt->taos->pAppInfo->serverCfg.sodInitial,
14,156✔
1348
                         .macMode = pStmt->taos->pAppInfo->serverCfg.macActive,
14,156✔
1349
                         .db = pStmt->exec.pRequest->pDb,
14,156✔
1350
                         .topicQuery = false,
1351
                         .pSql = pStmt->sql.sqlStr,
14,156✔
1352
                         .sqlLen = pStmt->sql.sqlLen,
14,156✔
1353
                         .pMsg = pStmt->exec.pRequest->msgBuf,
14,156✔
1354
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1355
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
14,156✔
1356
                         .pStmtCb = NULL,
1357
                         .pUser = pStmt->taos->user,
14,156✔
1358
                         .setQueryFp = setQueryRequest,
1359
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
14,156✔
1360

1361
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
14,156✔
1362
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
14,156✔
1363

1364
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery, NULL));
14,156✔
1365

1366
    if (pStmt->sql.pQuery->haveResultSet) {
14,156✔
1367
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
14,156✔
1368
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1369
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
14,156✔
1370
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
14,156✔
1371
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
14,156✔
1372
    }
1373

1374
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
14,156✔
1375
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
14,156✔
1376
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
14,156✔
1377

1378
    return TSDB_CODE_SUCCESS;
14,156✔
1379
  }
1380

1381
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
9,799,421✔
1382
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1383
  }
1384

1385
  STableDataCxt** pDataBlock = NULL;
9,802,394✔
1386

1387
  if (pStmt->exec.pCurrBlock) {
9,802,394✔
1388
    pDataBlock = &pStmt->exec.pCurrBlock;
2,239,849✔
1389
  } else {
1390
    pDataBlock =
1391
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
7,569,265✔
1392
    if (NULL == pDataBlock) {
7,585,527✔
1393
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1394
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1395
    }
1396
    pStmt->exec.pCurrBlock = *pDataBlock;
7,585,527✔
1397
    if (pStmt->sql.stbInterlaceMode) {
7,583,603✔
1398
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
6,358✔
1399
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
6,358✔
1400
    }
1401
  }
1402

1403
  int64_t startUs2 = taosGetTimestampUs();
9,814,138✔
1404
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
9,814,138✔
1405

1406
  SStmtQNode* param = NULL;
9,814,689✔
1407
  if (pStmt->sql.stbInterlaceMode) {
9,819,898✔
1408
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
4,434,177✔
1409
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
4,428,982✔
1410
    taosArrayClear(param->tblData.aCol);
2,213,428✔
1411

1412
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1413

1414
    param->restoreTbCols = false;
2,213,132✔
1415
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
2,213,474✔
1416
  }
1417

1418
  int64_t startUs3 = taosGetTimestampUs();
9,814,021✔
1419
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
9,814,021✔
1420

1421
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
9,806,494✔
1422

1423
  if (pStmt->sql.predicateCols) {
9,821,326✔
1424
    STMT_ERR_RET(qBindUpdateStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
990✔
1425
                                          pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt,
1426
                                          pStmt->sql.predicateCols));
1427
    return TSDB_CODE_SUCCESS;
990✔
1428
  }
1429

1430
  if (colIdx < 0) {
9,800,880✔
1431
    if (pStmt->sql.stbInterlaceMode) {
9,800,040✔
1432
      (*pDataBlock)->pData->flags = 0;
2,218,584✔
1433
      code =
1434
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
2,218,759✔
1435
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
2,219,138✔
1436
    } else {
1437
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
7,591,604✔
1438
                                pStmt->taos->optionInfo.charsetCxt);
7,591,495✔
1439
    }
1440

1441
    if (code) {
9,814,769✔
1442
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
4,494✔
1443
      STMT_ERR_RET(code);
4,494✔
1444
    }
1445
  } else {
1446
    if (pStmt->sql.stbInterlaceMode) {
840✔
1447
      tscError("bind single column not allowed in stb insert mode");
×
1448
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1449
    }
1450

1451
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
840✔
1452
      tscError("bind column index not in sequence");
×
1453
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1454
    }
1455

1456
    pStmt->bInfo.sBindLastIdx = colIdx;
840✔
1457

1458
    if (0 == colIdx) {
840✔
1459
      pStmt->bInfo.sBindRowNum = bind->num;
180✔
1460
    }
1461

1462
    code =
1463
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
840✔
1464
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
840✔
1465
    if (code) {
840✔
1466
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1467
      STMT_ERR_RET(code);
×
1468
    }
1469
  }
1470

1471
  int64_t startUs4 = taosGetTimestampUs();
9,821,013✔
1472
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
9,821,013✔
1473

1474
  if (pStmt->sql.stbInterlaceMode) {
9,821,956✔
1475
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
2,220,241✔
1476
  }
1477

1478
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
9,817,850✔
1479

1480
  return TSDB_CODE_SUCCESS;
9,818,467✔
1481
}
1482

1483
int stmtAddBatch(TAOS_STMT* stmt) {
7,944,280✔
1484
  STscStmt* pStmt = (STscStmt*)stmt;
7,944,280✔
1485

1486
  int64_t startUs = taosGetTimestampUs();
7,946,994✔
1487

1488
  STMT_DLOG_E("start to add batch");
7,946,994✔
1489

1490
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,947,006✔
1491
    return pStmt->errCode;
×
1492
  }
1493

1494
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
7,945,425✔
1495

1496
  if (pStmt->sql.stbInterlaceMode) {
7,943,893✔
1497
    int64_t startUs2 = taosGetTimestampUs();
342,291✔
1498
    pStmt->stat.addBatchUs += startUs2 - startUs;
342,291✔
1499

1500
    pStmt->sql.siInfo.tableColsReady = false;
342,232✔
1501

1502
    SStmtQNode* param = NULL;
342,291✔
1503
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
684,291✔
1504
    param->restoreTbCols = true;
342,169✔
1505
    param->next = NULL;
342,118✔
1506

1507
    stmtEnqueue(pStmt, param);
342,161✔
1508

1509
    return TSDB_CODE_SUCCESS;
342,342✔
1510
  }
1511

1512
  STMT_ERR_RET(stmtCacheBlock(pStmt));
7,600,178✔
1513

1514
  return TSDB_CODE_SUCCESS;
7,600,221✔
1515
}
1516
/*
1517
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1518
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1519

1520
  int32_t code = 0;
1521
  int32_t finalCode = 0;
1522
  size_t  keyLen = 0;
1523
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1524
  while (pIter) {
1525
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1526
    char*          key = taosHashGetKey(pIter, &keyLen);
1527

1528
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1529
    if (pMeta->uid) {
1530
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1531
      continue;
1532
    }
1533

1534
    SSubmitBlkRsp* blkRsp = NULL;
1535
    int32_t        i = 0;
1536
    for (; i < pRsp->nBlocks; ++i) {
1537
      blkRsp = pRsp->pBlocks + i;
1538
      if (strlen(blkRsp->tblFName) != keyLen) {
1539
        continue;
1540
      }
1541

1542
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1543
        continue;
1544
      }
1545

1546
      break;
1547
    }
1548

1549
    if (i < pRsp->nBlocks) {
1550
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1551
               blkRsp->uid);
1552

1553
      pMeta->uid = blkRsp->uid;
1554
      pStmt->bInfo.tbUid = blkRsp->uid;
1555
    } else {
1556
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1557
      if (NULL == pStmt->pCatalog) {
1558
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1559
        if (code) {
1560
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1561
          finalCode = code;
1562
          continue;
1563
        }
1564
      }
1565

1566
      code = stmtCreateRequest(pStmt);
1567
      if (code) {
1568
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1569
        finalCode = code;
1570
        continue;
1571
      }
1572

1573
      STableMeta*      pTableMeta = NULL;
1574
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1575
                               .requestId = pStmt->exec.pRequest->requestId,
1576
                               .requestObjRefId = pStmt->exec.pRequest->self,
1577
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1578
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1579

1580
      pStmt->stat.ctgGetTbMetaNum++;
1581

1582
      taos_free_result(pStmt->exec.pRequest);
1583
      pStmt->exec.pRequest = NULL;
1584

1585
      if (code || NULL == pTableMeta) {
1586
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1587
        finalCode = code;
1588
        taosMemoryFree(pTableMeta);
1589
        continue;
1590
      }
1591

1592
      pMeta->uid = pTableMeta->uid;
1593
      pStmt->bInfo.tbUid = pTableMeta->uid;
1594
      taosMemoryFree(pTableMeta);
1595
    }
1596

1597
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1598
  }
1599

1600
  return finalCode;
1601
}
1602
*/
1603

1604
/*
1605
int stmtStaticModeExec(TAOS_STMT* stmt) {
1606
  STscStmt*   pStmt = (STscStmt*)stmt;
1607
  int32_t     code = 0;
1608
  SSubmitRsp* pRsp = NULL;
1609
  if (pStmt->sql.staticMode) {
1610
    return TSDB_CODE_TSC_STMT_API_ERROR;
1611
  }
1612

1613
  STMT_DLOG_E("start to exec");
1614

1615
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1616

1617
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1618
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1619

1620
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1621

1622
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1623
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1624
    if (code) {
1625
      pStmt->exec.pRequest->code = code;
1626
    } else {
1627
      tFreeSSubmitRsp(pRsp);
1628
      STMT_ERR_RET(stmtResetStmt(pStmt));
1629
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1630
    }
1631
  }
1632

1633
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1634

1635
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1636
  pStmt->affectedRows += pStmt->exec.affectedRows;
1637

1638
_return:
1639

1640
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1641

1642
  tFreeSSubmitRsp(pRsp);
1643

1644
  ++pStmt->sql.runTimes;
1645

1646
  STMT_RET(code);
1647
}
1648
*/
1649

1650
int stmtExec(TAOS_STMT* stmt) {
7,937,157✔
1651
  STscStmt*   pStmt = (STscStmt*)stmt;
7,937,157✔
1652
  int32_t     code = 0;
7,937,157✔
1653
  SSubmitRsp* pRsp = NULL;
7,937,157✔
1654

1655
  int64_t startUs = taosGetTimestampUs();
7,943,204✔
1656

1657
  STMT_DLOG_E("start to exec");
7,943,204✔
1658

1659
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,943,204✔
1660
    return pStmt->errCode;
×
1661
  }
1662

1663
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
7,934,782✔
1664

1665
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
7,938,155✔
1666
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
14,156✔
1667
  } else {
1668
    if (pStmt->sql.stbInterlaceMode) {
7,921,367✔
1669
      int64_t startTs = taosGetTimestampUs();
342,244✔
1670
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
1,120,752✔
1671
        taosUsleep(1);
778,508✔
1672
      }
1673
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
342,163✔
1674

1675
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
342,214✔
1676
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
342,201✔
1677
      pStmt->sql.siInfo.pVgroupHash = NULL;
342,195✔
1678
      pStmt->sql.siInfo.pVgroupList = NULL;
342,303✔
1679
    } else {
1680
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
7,583,621✔
1681
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
7,576,901✔
1682

1683
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
7,578,050✔
1684

1685
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
7,584,417✔
1686
    }
1687

1688
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
7,920,305✔
1689
  }
1690

1691
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
7,948,933✔
1692
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1693
    if (code) {
×
1694
      pStmt->exec.pRequest->code = code;
×
1695
    } else {
1696
      tFreeSSubmitRsp(pRsp);
×
1697
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1698
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1699
    }
1700
  }
1701

1702
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
7,949,598✔
1703

1704
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
7,947,972✔
1705
  pStmt->affectedRows += pStmt->exec.affectedRows;
7,947,110✔
1706

1707
_return:
7,948,488✔
1708

1709
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
7,952,913✔
1710
    taosUsleep(1);
4,425✔
1711
  }
1712

1713
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
7,946,869✔
1714

1715
  tFreeSSubmitRsp(pRsp);
7,948,989✔
1716

1717
  ++pStmt->sql.runTimes;
7,947,420✔
1718

1719
  int64_t startUs2 = taosGetTimestampUs();
7,946,793✔
1720
  pStmt->stat.execUseUs += startUs2 - startUs;
7,946,793✔
1721

1722
  STMT_RET(code);
7,948,466✔
1723
}
1724

1725
int stmtClose(TAOS_STMT* stmt) {
45,073✔
1726
  STscStmt* pStmt = (STscStmt*)stmt;
45,073✔
1727

1728
  STMT_DLOG_E("start to free stmt");
45,073✔
1729

1730
  if (pStmt->bindThreadInUse) {
45,073✔
1731
    pStmt->queue.stopQueue = true;
14,056✔
1732

1733
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
14,056✔
1734
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
14,056✔
1735
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
14,056✔
1736
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
14,056✔
1737

1738
    (void)taosThreadJoin(pStmt->bindThread, NULL);
14,056✔
1739
    pStmt->bindThreadInUse = false;
14,056✔
1740

1741
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
14,056✔
1742
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
14,056✔
1743
  }
1744

1745
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
45,073✔
1746
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1747
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1748
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1749
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1750
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1751
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1752
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1753
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1754
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1755

1756
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
45,073✔
1757
  taosMemoryFree(stmt);
45,073✔
1758

1759
  return TSDB_CODE_SUCCESS;
45,073✔
1760
}
1761

1762
const char* stmtErrstr(TAOS_STMT* stmt) {
14,557✔
1763
  STscStmt* pStmt = (STscStmt*)stmt;
14,557✔
1764

1765
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
14,557✔
1766
    return (char*)tstrerror(terrno);
662✔
1767
  }
1768

1769
  pStmt->exec.pRequest->code = terrno;
13,895✔
1770

1771
  return taos_errstr(pStmt->exec.pRequest);
13,895✔
1772
}
1773

1774
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
3,428✔
1775

1776
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
5,634✔
1777

1778
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
19,574,597✔
1779
  STscStmt* pStmt = (STscStmt*)stmt;
19,574,597✔
1780

1781
  STMT_DLOG_E("start is insert");
19,574,597✔
1782

1783
  if (pStmt->sql.type) {
19,574,627✔
1784
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
12,000,673✔
1785
  } else {
1786
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
7,581,161✔
1787
  }
1788

1789
  return TSDB_CODE_SUCCESS;
19,581,999✔
1790
}
1791

1792
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
792✔
1793
  int32_t   code = 0;
792✔
1794
  STscStmt* pStmt = (STscStmt*)stmt;
792✔
1795
  int32_t   preCode = pStmt->errCode;
792✔
1796

1797
  STMT_DLOG_E("start to get tag fields");
792✔
1798

1799
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
792✔
1800
    return pStmt->errCode;
×
1801
  }
1802

1803
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
792✔
1804
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1805
  }
1806

1807
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
792✔
1808

1809
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
792✔
1810
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1811
    pStmt->bInfo.needParse = false;
×
1812
  }
1813

1814
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
792✔
1815
    taos_free_result(pStmt->exec.pRequest);
×
1816
    pStmt->exec.pRequest = NULL;
×
1817
  }
1818

1819
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
792✔
1820

1821
  if (pStmt->bInfo.needParse) {
792✔
1822
    STMT_ERRI_JRET(stmtParseSql(pStmt));
594✔
1823
  }
1824

1825
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
396✔
1826

1827
_return:
198✔
1828
  // compatible with previous versions
1829
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
792✔
1830
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
198✔
1831
  }
1832

1833
  if (code != TSDB_CODE_SUCCESS) {
792✔
1834
    taos_free_result(pStmt->exec.pRequest);
594✔
1835
    pStmt->exec.pRequest = NULL;
594✔
1836
  }
1837
  pStmt->errCode = preCode;
792✔
1838

1839
  return code;
792✔
1840
}
1841

1842
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
1,188✔
1843
  int32_t   code = 0;
1,188✔
1844
  STscStmt* pStmt = (STscStmt*)stmt;
1,188✔
1845
  int32_t   preCode = pStmt->errCode;
1,188✔
1846

1847
  STMT_DLOG_E("start to get col fields");
1,188✔
1848

1849
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,188✔
1850
    return pStmt->errCode;
×
1851
  }
1852

1853
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,188✔
1854
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1855
  }
1856

1857
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
1,188✔
1858

1859
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,188✔
1860
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1861
    pStmt->bInfo.needParse = false;
×
1862
  }
1863

1864
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,188✔
1865
    taos_free_result(pStmt->exec.pRequest);
×
1866
    pStmt->exec.pRequest = NULL;
×
1867
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1868
  }
1869

1870
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
1,188✔
1871

1872
  if (pStmt->bInfo.needParse) {
1,188✔
1873
    STMT_ERRI_JRET(stmtParseSql(pStmt));
990✔
1874
  }
1875

1876
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
1,188✔
1877

1878
_return:
1,188✔
1879

1880
  if (code != TSDB_CODE_SUCCESS) {
1,188✔
1881
    taos_free_result(pStmt->exec.pRequest);
×
1882
    pStmt->exec.pRequest = NULL;
×
1883
  }
1884

1885
  pStmt->errCode = preCode;
1,188✔
1886

1887
  return code;
1,188✔
1888
}
1889

1890
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
396✔
1891
  int       code = 0;
396✔
1892
  STscStmt* pStmt = (STscStmt*)stmt;
396✔
1893
  int32_t   preCode = pStmt->errCode;
396✔
1894

1895
  STMT_DLOG_E("start to get param num");
396✔
1896

1897
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
396✔
1898
    return pStmt->errCode;
×
1899
  }
1900

1901
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
396✔
1902

1903
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
396✔
1904
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1905
    pStmt->bInfo.needParse = false;
×
1906
  }
1907

1908
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
396✔
1909
    taos_free_result(pStmt->exec.pRequest);
×
1910
    pStmt->exec.pRequest = NULL;
×
1911
  }
1912

1913
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
396✔
1914

1915
  if (pStmt->bInfo.needParse) {
396✔
1916
    STMT_ERRI_JRET(stmtParseSql(pStmt));
198✔
1917
  }
1918

1919
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
198✔
1920
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1921
  } else {
1922
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
198✔
1923
  }
1924

1925
_return:
198✔
1926

1927
  if (code != TSDB_CODE_SUCCESS) {
396✔
1928
    taos_free_result(pStmt->exec.pRequest);
198✔
1929
    pStmt->exec.pRequest = NULL;
198✔
1930
  }
1931

1932
  pStmt->errCode = preCode;
396✔
1933

1934
  return code;
396✔
1935
}
1936

1937
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
792✔
1938
  int       code = 0;
792✔
1939
  STscStmt* pStmt = (STscStmt*)stmt;
792✔
1940
  int32_t   preCode = pStmt->errCode;
792✔
1941

1942
  STMT_DLOG_E("start to get param");
792✔
1943

1944
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
792✔
1945
    return pStmt->errCode;
×
1946
  }
1947

1948
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
792✔
1949
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1950
  }
1951

1952
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
792✔
1953

1954
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
792✔
1955
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1956
    pStmt->bInfo.needParse = false;
×
1957
  }
1958

1959
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
792✔
1960
    taos_free_result(pStmt->exec.pRequest);
×
1961
    pStmt->exec.pRequest = NULL;
×
1962
  }
1963

1964
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
792✔
1965

1966
  if (pStmt->bInfo.needParse) {
792✔
1967
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1968
  }
1969

1970
  int32_t       nums = 0;
792✔
1971
  TAOS_FIELD_E* pField = NULL;
792✔
1972
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
792✔
1973
  if (idx >= nums) {
792✔
1974
    tscError("idx %d is too big", idx);
×
1975
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1976
  }
1977

1978
  *type = pField[idx].type;
792✔
1979
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
792✔
1980

1981
_return:
792✔
1982

1983
  if (code != TSDB_CODE_SUCCESS) {
792✔
1984
    taos_free_result(pStmt->exec.pRequest);
×
1985
    pStmt->exec.pRequest = NULL;
×
1986
  }
1987

1988
  taosMemoryFree(pField);
792✔
1989
  pStmt->errCode = preCode;
792✔
1990

1991
  return code;
792✔
1992
}
1993

1994
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
14,156✔
1995
  STscStmt* pStmt = (STscStmt*)stmt;
14,156✔
1996

1997
  STMT_DLOG_E("start to use result");
14,156✔
1998

1999
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
14,156✔
2000
    tscError("useResult only for query statement");
×
2001
    return NULL;
×
2002
  }
2003

2004
  return pStmt->exec.pRequest;
14,156✔
2005
}
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