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

taosdata / TDengine / #4836

31 Oct 2025 03:37AM UTC coverage: 58.728% (+0.2%) from 58.506%
#4836

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149727 of 324176 branches covered (46.19%)

Branch coverage included in aggregate %.

198923 of 269498 relevant lines covered (73.81%)

238054213.11 hits per line

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

53.49
/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,303,479!
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
17,305,012✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
17,303,762✔
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;
17,300,325✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
17,307,438✔
44
  int i = 0;
17,307,438✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
91,312,096✔
46
    if (i < 10) {
74,016,918✔
47
      taosUsleep(1);
67,828,919✔
48
      i++;
67,816,659✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
6,187,999✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
6,187,999✔
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
6,186,828✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
6,187,999✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
17,302,937✔
58
    return false;
109,002✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
17,192,764✔
61
  SStmtQNode* node = pStmt->queue.head->next;
17,192,764✔
62
  pStmt->queue.head = pStmt->queue.head->next;
17,193,935✔
63
  *param = node;
17,193,935✔
64

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

67
  return true;
17,201,655✔
68
}
69

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

74
  pStmt->stat.bindDataNum++;
17,199,200✔
75

76
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
17,199,200✔
77
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
17,202,478✔
78
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
17,201,207✔
79
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
17,200,955✔
80
}
17,199,313✔
81

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
242,903,613✔
83
  int32_t code = 0;
242,903,613✔
84

85
  if (pStmt->exec.pRequest == NULL) {
242,903,613✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
9,574,839✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
9,568,987!
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
9,569,814✔
92
      pStmt->exec.pRequest->syncQuery = true;
9,568,160✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
9,569,581✔
94
    }
95
  }
96

97
  return code;
243,075,228✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
465,371,799✔
101
  int32_t code = 0;
465,371,799✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
465,371,799!
104
    STMT_LOG_SEQ(newStatus);
468,534,001✔
105
  }
106

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

112
  switch (newStatus) {
469,135,682!
113
    case STMT_PREPARE:
9,516,557✔
114
      pStmt->errCode = 0;
9,516,557✔
115
      break;
8,525,409✔
116
    case STMT_SETTBNAME:
19,148,966✔
117
      if (STMT_STATUS_EQ(INIT)) {
19,148,966!
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
19,148,966!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
19,147,312✔
124
    case STMT_SETTAGS:
20,604✔
125
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
20,604!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
20,604✔
129
    case STMT_FETCH_FIELDS:
×
130
      if (STMT_STATUS_EQ(INIT)) {
×
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      break;
×
134
    case STMT_BIND:
214,652,238✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
214,652,238!
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;
214,999,131✔
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:
210,015,193✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
210,015,193!
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
210,319,527✔
154
    case STMT_EXECUTE:
15,782,124✔
155
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,782,124✔
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
26,886!
157
            STMT_STATUS_NE(BIND_COL)) {
×
158
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
159
        }
160
      } else {
161
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
15,752,896!
162
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
        }
164
      }
165
      break;
15,784,084✔
166
    default:
×
167
      code = TSDB_CODE_APP_ERROR;
×
168
      break;
×
169
  }
170

171
  STMT_ERR_RET(code);
468,748,348!
172

173
  pStmt->sql.status = newStatus;
468,748,348✔
174

175
  return TSDB_CODE_SUCCESS;
468,811,492✔
176
}
177

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

181
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
8,389,720✔
182

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

188
  *tbName = pStmt->bInfo.tbName;
8,391,374✔
189

190
  return TSDB_CODE_SUCCESS;
8,392,201✔
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,413,143✔
243
                           bool autoCreateTbl, uint8_t tbNameFlag) {
244
  STscStmt* pStmt = (STscStmt*)stmt;
9,413,143✔
245
  char      tbFName[TSDB_TABLE_FNAME_LEN];
9,370,956✔
246
  int32_t   code = tNameExtractFullName(tbName, tbFName);
9,419,463✔
247
  if (code != 0) {
9,423,883!
248
    return code;
×
249
  }
250

251
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
9,423,883!
252
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
9,418,921!
253
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
9,418,441✔
254

255
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
9,420,095✔
256
  pStmt->bInfo.tbSuid = pTableMeta->suid;
9,418,028✔
257
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
9,416,961✔
258
  pStmt->bInfo.tbType = pTableMeta->tableType;
9,419,738✔
259
  pStmt->bInfo.boundTags = tags;
9,419,748✔
260
  pStmt->bInfo.tagsCached = false;
9,420,165✔
261
  pStmt->bInfo.tbNameFlag = tbNameFlag;
9,427,487✔
262
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
9,421,402!
263

264
  return TSDB_CODE_SUCCESS;
9,403,033✔
265
}
266

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

270
  pStmt->sql.pVgHash = pVgHash;
9,408,712✔
271
  pStmt->exec.pBlockHash = pBlockHash;
9,418,462✔
272

273
  return TSDB_CODE_SUCCESS;
9,420,571✔
274
}
275

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

281
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
9,412,847!
282
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
9,407,773!
283

284
  pStmt->sql.autoCreateTbl = autoCreateTbl;
9,419,344✔
285
  if (pStmt->sql.autoCreateTbl) {
9,419,321✔
286
    pStmt->sql.stbInterlaceMode = false;
8,289,279✔
287
  }
288

289
  return TSDB_CODE_SUCCESS;
9,418,532✔
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) {
203,241,382✔
305
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
203,241,382✔
306
    return TSDB_CODE_SUCCESS;
195,701,267✔
307
  }
308

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

312
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
8,337,074✔
313
    return TSDB_CODE_SUCCESS;
44,396✔
314
  }
315

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

322
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
8,300,857!
323

324
  SStmtTableCache cache = {
8,299,203✔
325
      .pDataCtx = pDst,
326
      .boundTags = pStmt->bInfo.boundTags,
8,300,030✔
327
  };
328

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

333
  if (pStmt->sql.autoCreateTbl) {
8,300,030!
334
    pStmt->bInfo.tagsCached = true;
8,301,684✔
335
  } else {
336
    pStmt->bInfo.boundTags = NULL;
×
337
  }
338

339
  return TSDB_CODE_SUCCESS;
8,300,030✔
340
}
341

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

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

352
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,458,504!
353
  pStmt->exec.pRequest->stmtBindVersion = 1;
9,451,619✔
354

355
  pStmt->stat.parseSqlNum++;
9,458,235✔
356
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
9,458,504✔
357
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
9,440,290✔
358

359
  pStmt->bInfo.needParse = false;
9,448,897✔
360

361
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
9,448,257✔
362
    pStmt->sql.type = STMT_TYPE_INSERT;
1,036,519✔
363
    pStmt->sql.stbInterlaceMode = false;
1,037,791✔
364
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
8,407,094✔
365
    pStmt->sql.type = STMT_TYPE_QUERY;
26,886✔
366
    pStmt->sql.stbInterlaceMode = false;
26,886✔
367

368
    return TSDB_CODE_SUCCESS;
26,886✔
369
  }
370

371
  STableDataCxt** pSrc =
372
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
9,420,573!
373
  if (NULL == pSrc || NULL == *pSrc) {
9,429,936!
374
    return terrno;
1,474✔
375
  }
376

377
  STableDataCxt* pTableCtx = *pSrc;
9,428,462✔
378
  if (pStmt->sql.stbInterlaceMode) {
9,421,846✔
379
    int16_t lastIdx = -1;
90,235✔
380

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

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

391
  if (NULL == pStmt->sql.pBindInfo) {
9,425,354!
392
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
9,426,180!
393
    if (NULL == pStmt->sql.pBindInfo) {
9,423,725!
394
      return terrno;
×
395
    }
396
  }
397

398
  return TSDB_CODE_SUCCESS;
9,422,525✔
399
}
400

401
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
25,417,646✔
402
  pStmt->bInfo.tbUid = 0;
25,417,646✔
403
  pStmt->bInfo.tbSuid = 0;
25,420,530✔
404
  pStmt->bInfo.tbVgId = -1;
25,418,876✔
405
  pStmt->bInfo.tbType = 0;
25,417,713✔
406
  pStmt->bInfo.needParse = true;
25,414,405✔
407
  pStmt->bInfo.inExecCache = false;
25,417,772✔
408

409
  pStmt->bInfo.tbName[0] = 0;
25,416,609✔
410
  pStmt->bInfo.tbFName[0] = 0;
25,419,980✔
411
  if (!pStmt->bInfo.tagsCached) {
25,419,644✔
412
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
8,813,573✔
413
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
8,815,209!
414
  }
415
  pStmt->bInfo.stbFName[0] = 0;
25,417,351✔
416

417
  return TSDB_CODE_SUCCESS;
25,418,178✔
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,374,879✔
426
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
6,374,879✔
427
  if (NULL == pTblBuf->pCurBuff) {
6,376,441✔
428
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
616!
429
    return;
×
430
  }
431
  pTblBuf->buffIdx = 1;
6,375,825✔
432
  pTblBuf->buffOffset = sizeof(*pQueue->head);
6,375,825✔
433

434
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
6,375,825✔
435
  pQueue->qRemainNum = 0;
6,375,825✔
436
  pQueue->head->next = NULL;
6,374,662✔
437
}
438

439
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
25,299,064✔
440
  if (pStmt->sql.stbInterlaceMode) {
25,299,064✔
441
    if (deepClean) {
6,466,764✔
442
      taosHashCleanup(pStmt->exec.pBlockHash);
91,508✔
443
      pStmt->exec.pBlockHash = NULL;
91,508✔
444

445
      if (NULL != pStmt->exec.pCurrBlock) {
91,508!
446
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
91,508!
447
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
91,508✔
448
      }
449
    } else {
450
      pStmt->sql.siInfo.pTableColsIdx = 0;
6,375,256✔
451
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
6,375,256✔
452
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
6,375,979✔
453
    }
454
  } else {
455
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
18,834,525✔
456
      taos_free_result(pStmt->exec.pRequest);
18,810,947✔
457
      pStmt->exec.pRequest = NULL;
18,807,939✔
458
    }
459

460
    size_t keyLen = 0;
18,834,380✔
461
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
18,836,034✔
462
    while (pIter) {
37,570,043✔
463
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
18,729,811✔
464
      char*          key = taosHashGetKey(pIter, &keyLen);
18,729,811✔
465
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
18,727,863✔
466

467
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
18,729,811✔
468
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
9,382,535✔
469
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
9,427,317!
470

471
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
9,376,280✔
472
        continue;
9,381,708✔
473
      }
474

475
      qDestroyStmtDataBlock(pBlocks);
9,347,276✔
476
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
9,346,004!
477

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

481
    if (keepTable) {
18,840,232✔
482
      return TSDB_CODE_SUCCESS;
9,407,767✔
483
    }
484

485
    taosHashCleanup(pStmt->exec.pBlockHash);
9,432,465✔
486
    pStmt->exec.pBlockHash = NULL;
9,431,638✔
487

488
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
9,431,638✔
489
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
9,432,465!
490
  }
491

492
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
15,899,487✔
493

494
  return TSDB_CODE_SUCCESS;
15,896,604✔
495
}
496

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

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

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

510
  taosMemoryFree(pStmt->sql.pBindInfo);
9,515,955!
511
  taosMemoryFree(pStmt->sql.queryRes.fields);
9,520,002!
512
  taosMemoryFree(pStmt->sql.queryRes.userFields);
9,521,656!
513
  taosMemoryFree(pStmt->sql.sqlStr);
9,518,604!
514
  qDestroyQuery(pStmt->sql.pQuery);
9,519,708✔
515
  taosArrayDestroy(pStmt->sql.nodeList);
9,520,384✔
516
  taosHashCleanup(pStmt->sql.pVgHash);
9,519,175✔
517
  pStmt->sql.pVgHash = NULL;
9,522,483✔
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
9,522,483✔
520
  while (pIter) {
17,819,969✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
8,300,030✔
522

523
    qDestroyStmtDataBlock(pCache->pDataCtx);
8,300,030✔
524
    qDestroyBoundColInfo(pCache->boundTags);
8,300,857✔
525
    taosMemoryFreeClear(pCache->boundTags);
8,295,895!
526

527
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
8,298,376✔
528
  }
529
  taosHashCleanup(pStmt->sql.pTableCache);
9,519,939✔
530
  pStmt->sql.pTableCache = NULL;
9,521,656✔
531

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
9,519,557!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
9,520,415!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
9,521,519✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
9,521,242✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
9,522,346✔
538
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
9,519,280✔
539
  tSimpleHashCleanup(pStmt->sql.predicateCols);
9,519,557✔
540
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
9,518,176✔
541
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
9,519,557!
542
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
9,518,344✔
543
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
9,518,176✔
544

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

548
  STMT_DLOG_E("end to free SQL info");
9,514,209✔
549

550
  return TSDB_CODE_SUCCESS;
9,518,071✔
551
}
552

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

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

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

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

575
  *vgId = vgInfo.vgId;
7,368✔
576

577
  return TSDB_CODE_SUCCESS;
7,368✔
578
}
579

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

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

587
  return TSDB_CODE_SUCCESS;
7,368✔
588
}
589

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

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

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

607
  return TSDB_CODE_SUCCESS;
×
608
}
609

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

617
  pStmt->bInfo.needParse = true;
8,418,734✔
618
  pStmt->bInfo.inExecCache = false;
8,412,620✔
619

620
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
8,418,734!
621
  if (pCxtInExec) {
8,420,388✔
622
    pStmt->bInfo.needParse = false;
18,448✔
623
    pStmt->bInfo.inExecCache = true;
18,448✔
624

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

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

633
  if (NULL == pStmt->pCatalog) {
8,401,940✔
634
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
123,618!
635
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
123,728✔
636
  }
637

638
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
8,400,071✔
639
    if (pStmt->bInfo.inExecCache) {
8,396,011!
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,393,855✔
646
    return TSDB_CODE_SUCCESS;
8,391,049✔
647
  }
648

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

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

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

663
      pStmt->exec.pCurrBlock = pNewBlock;
7,368✔
664

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

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

684
  pStmt->stat.ctgGetTbMetaNum++;
×
685

686
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
×
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);
×
694

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

701
  taosMemoryFree(pTableMeta);
×
702

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

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

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

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

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

753
    pStmt->exec.pCurrBlock = pNewBlock;
×
754

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

757
    return TSDB_CODE_SUCCESS;
×
758
  }
759

760
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
761

762
  return TSDB_CODE_SUCCESS;
×
763
}
764

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

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

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

777
  pStmt->sql.status = STMT_INIT;
8,267,646✔
778

779
  return TSDB_CODE_SUCCESS;
8,269,300✔
780
}
781

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

785
  if (pParam->restoreTbCols) {
17,200,308✔
786
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
17,195,674✔
787
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
10,821,112✔
788
      *p = taosArrayInit(20, POINTER_BYTES);
10,823,454✔
789
      if (*p == NULL) {
10,821,744!
790
        STMT_ERR_RET(terrno);
×
791
      }
792
    }
793

794
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
6,375,733✔
795
  } else {
796
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
10,823,613!
797
                                        &pStmt->sql.siInfo));
798

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

801
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
10,825,325✔
802
  }
803
  return TSDB_CODE_SUCCESS;
17,203,374✔
804
}
805

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

809
  qInfo("stmt bind thread started");
109,002!
810

811
  STscStmt* pStmt = (STscStmt*)param;
109,002✔
812

813
  while (true) {
17,312,475✔
814
    if (pStmt->queue.stopQueue) {
17,421,477✔
815
      break;
109,002✔
816
    }
817

818
    SStmtQNode* asyncParam = NULL;
17,311,304✔
819
    if (!stmtDequeue(pStmt, &asyncParam)) {
17,311,304✔
820
      continue;
109,002✔
821
    }
822

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

829
  qInfo("stmt bind thread stopped");
109,002!
830

831
  return NULL;
109,002✔
832
}
833

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

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

848
  pStmt->bindThreadInUse = true;
109,002✔
849

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

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

860
  return TSDB_CODE_SUCCESS;
109,002✔
861
}
862

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

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

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

883
  return TSDB_CODE_SUCCESS;
109,002✔
884
}
885

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

891
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
1,245,921!
892
  if (NULL == pStmt) {
1,251,529!
893
    return NULL;
×
894
  }
895

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

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

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

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

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

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

953
  STMT_LOG_SEQ(STMT_INIT);
1,251,529✔
954

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

957
  return pStmt;
1,251,529✔
958
}
959

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

963
  STMT_DLOG_E("start to prepare");
9,517,247✔
964

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

969
  if (pStmt->sql.status >= STMT_PREPARE) {
9,522,346✔
970
    STMT_ERR_RET(stmtResetStmt(pStmt));
8,270,954!
971
  }
972

973
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
9,519,865!
974

975
  if (length <= 0) {
9,516,941✔
976
    length = strlen(sql);
1,092,558!
977
  }
978

979
  pStmt->sql.sqlStr = taosStrndup(sql, length);
9,516,941!
980
  if (!pStmt->sql.sqlStr) {
9,521,382!
981
    return terrno;
×
982
  }
983
  pStmt->sql.sqlLen = length;
9,519,728✔
984
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
9,518,901!
985

986
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,522,209!
987

988
  int32_t code = 0;
9,514,595✔
989
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
18,993,006!
990
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
18,992,453✔
991
    // get table meta
992
    STableMeta* pTableMeta = NULL;
×
993
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
×
994

995
    char* newSql = NULL;
×
996

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

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

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

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

1027
  STMT_ERR_RET(code);
9,514,703!
1028

1029
  char* dbName = NULL;
9,514,703✔
1030
  if (qParseDbName(sql, length, &dbName)) {
9,514,703✔
1031
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
1,086,468✔
1032
    taosMemoryFreeClear(dbName);
1,085,641!
1033
  }
1034

1035
  return TSDB_CODE_SUCCESS;
9,514,013✔
1036
}
1037

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

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

1048
  SArray* pTblCols = NULL;
90,224✔
1049
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
90,870,820✔
1050
    pTblCols = taosArrayInit(20, POINTER_BYTES);
90,779,320✔
1051
    if (NULL == pTblCols) {
90,156,086!
1052
      return terrno;
×
1053
    }
1054

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

1060
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
91,500✔
1061

1062
  return TSDB_CODE_SUCCESS;
91,500✔
1063
}
1064

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

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

1070
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,085,059!
1071

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

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

1084
  int64_t startUs = taosGetTimestampUs();
19,154,070✔
1085

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

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

1092
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
19,150,892!
1093

1094
  int32_t insert = 0;
19,151,595✔
1095
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
19,147,943!
1096
  if (0 == insert) {
19,145,470!
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,145,470✔
1102
    STMT_ERR_RET(stmtCreateRequest(pStmt));
8,412,812!
1103

1104
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
8,420,498!
1105
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1106
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
8,417,190!
1107

1108
    STMT_ERR_RET(stmtGetFromCache(pStmt));
8,413,882!
1109

1110
    if (pStmt->bInfo.needParse) {
8,416,363✔
1111
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
8,387,741!
1112
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
8,387,239✔
1113

1114
      STMT_ERR_RET(stmtParseSql(pStmt));
8,388,066✔
1115
    }
1116
  } else {
1117
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
10,728,179!
1118
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
10,729,350✔
1119
    pStmt->exec.pRequest->requestId++;
10,729,350✔
1120
    pStmt->bInfo.needParse = false;
10,731,692✔
1121
  }
1122

1123
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
19,133,370✔
1124
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
90,334!
1125
  }
1126

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

1130
  return TSDB_CODE_SUCCESS;
19,149,416✔
1131
}
1132

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

1136
  STMT_DLOG_E("start to set tbTags");
20,604✔
1137

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

1142
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
20,604!
1143

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

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

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

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

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

1188
  return TSDB_CODE_SUCCESS;
×
1189
}
1190

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

1196
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
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;
×
1202

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

1219
  return TSDB_CODE_SUCCESS;
×
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) {
10,819,965✔
1241
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
10,819,965✔
1242
    pStmt->sql.siInfo.pVgroupHash =
6,375,601✔
1243
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
6,376,595✔
1244
  }
1245
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
10,822,484✔
1246
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
6,375,601✔
1247
  }
1248

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

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

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

1262
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
10,823,478✔
1263
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
1,305,014!
1264
    pStmt->sql.siInfo.tbFromHash = true;
25,713✔
1265
  }
1266

1267
  if (0 == pStmt->sql.siInfo.firstName[0]) {
10,823,478✔
1268
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
91,508!
1269
  }
1270

1271
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
10,823,478!
1272
  param->next = NULL;
10,823,478✔
1273

1274
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
10,823,478✔
1275

1276
  stmtEnqueue(pStmt, param);
10,824,583✔
1277

1278
  return TSDB_CODE_SUCCESS;
10,823,521✔
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)) {
10,819,505!
1284
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
10,819,309✔
1285
      break;
10,821,613✔
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;
10,821,613✔
1302
}
1303

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

1308
  int64_t startUs = taosGetTimestampUs();
214,105,094✔
1309

1310
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
214,105,094✔
1311

1312
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
215,218,584!
1313
    return pStmt->errCode;
×
1314
  }
1315

1316
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
215,407,791!
1317

1318
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
215,316,575!
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) {
215,148,527!
1324
    taos_free_result(pStmt->exec.pRequest);
×
1325
    pStmt->exec.pRequest = NULL;
×
1326
  }
1327

1328
  STMT_ERR_RET(stmtCreateRequest(pStmt));
215,175,572!
1329

1330
  if (pStmt->bInfo.needParse) {
214,438,303✔
1331
    STMT_ERR_RET(stmtParseSql(pStmt));
1,068,784✔
1332
  }
1333

1334
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
215,512,924✔
1335
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
26,886!
1336

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

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

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

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

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

1368
    return TSDB_CODE_SUCCESS;
26,886✔
1369
  }
1370

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

1375
  STableDataCxt** pDataBlock = NULL;
214,074,051✔
1376

1377
  if (pStmt->exec.pCurrBlock) {
214,074,051✔
1378
    pDataBlock = &pStmt->exec.pCurrBlock;
204,917,466✔
1379
  } else {
1380
    pDataBlock =
1381
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
9,428,250!
1382
    if (NULL == pDataBlock) {
9,431,416!
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,431,416✔
1387
    if (pStmt->sql.stbInterlaceMode) {
9,430,242✔
1388
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
90,334✔
1389
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
91,508✔
1390
    }
1391
  }
1392

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

1396
  SStmtQNode* param = NULL;
213,455,098✔
1397
  if (pStmt->sql.stbInterlaceMode) {
213,642,553✔
1398
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
21,640,800!
1399
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
21,641,118!
1400
    taosArrayClear(param->tblData.aCol);
10,821,613✔
1401

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

1404
    param->restoreTbCols = false;
10,818,883✔
1405
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
10,816,538!
1406
  }
1407

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

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

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

1420
  if (colIdx < 0) {
213,824,226!
1421
    if (pStmt->sql.stbInterlaceMode) {
214,619,656✔
1422
      (*pDataBlock)->pData->flags = 0;
10,820,299✔
1423
      code =
1424
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
10,822,603✔
1425
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
10,821,470✔
1426
    } else {
1427
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
204,099,306✔
1428
                                pStmt->taos->optionInfo.charsetCxt);
203,834,713✔
1429
    }
1430

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

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

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

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

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

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

1464
  if (pStmt->sql.stbInterlaceMode) {
214,816,331✔
1465
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
10,823,978!
1466
  }
1467

1468
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
213,312,567✔
1469

1470
  return TSDB_CODE_SUCCESS;
213,578,427✔
1471
}
1472

1473
int stmtAddBatch(TAOS_STMT* stmt) {
208,007,488✔
1474
  STscStmt* pStmt = (STscStmt*)stmt;
208,007,488✔
1475

1476
  int64_t startUs = taosGetTimestampUs();
210,510,523✔
1477

1478
  STMT_DLOG_E("start to add batch");
210,510,523✔
1479

1480
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
209,923,473!
1481
    return pStmt->errCode;
×
1482
  }
1483

1484
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
209,677,597!
1485

1486
  if (pStmt->sql.stbInterlaceMode) {
210,996,952✔
1487
    int64_t startUs2 = taosGetTimestampUs();
6,376,695✔
1488
    pStmt->stat.addBatchUs += startUs2 - startUs;
6,376,695✔
1489

1490
    pStmt->sql.siInfo.tableColsReady = false;
6,376,695✔
1491

1492
    SStmtQNode* param = NULL;
6,375,562✔
1493
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
12,748,513!
1494
    param->restoreTbCols = true;
6,372,989✔
1495
    param->next = NULL;
6,372,989✔
1496

1497
    stmtEnqueue(pStmt, param);
6,374,160✔
1498

1499
    return TSDB_CODE_SUCCESS;
6,375,601✔
1500
  }
1501

1502
  STMT_ERR_RET(stmtCacheBlock(pStmt));
204,441,300!
1503

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

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

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

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

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

1536
      break;
1537
    }
1538

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

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

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

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

1570
      pStmt->stat.ctgGetTbMetaNum++;
1571

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

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

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

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

1590
  return finalCode;
1591
}
1592
*/
1593

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

1603
  STMT_DLOG_E("start to exec");
1604

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

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

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

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

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

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

1628
_return:
1629

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

1632
  tFreeSSubmitRsp(pRsp);
1633

1634
  ++pStmt->sql.runTimes;
1635

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

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

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

1647
  STMT_DLOG_E("start to exec");
15,785,723✔
1648

1649
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,785,723!
1650
    return pStmt->errCode;
×
1651
  }
1652

1653
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
15,785,202!
1654

1655
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,783,795✔
1656
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
26,886✔
1657
  } else {
1658
    if (pStmt->sql.stbInterlaceMode) {
15,755,432✔
1659
      int64_t startTs = taosGetTimestampUs();
6,376,772✔
1660
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
17,297,770✔
1661
        taosUsleep(1);
10,920,003✔
1662
      }
1663
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
6,376,672✔
1664

1665
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
6,376,672!
1666
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
6,375,502✔
1667
      pStmt->sql.siInfo.pVgroupHash = NULL;
6,375,601✔
1668
      pStmt->sql.siInfo.pVgroupList = NULL;
6,375,601✔
1669
    } else {
1670
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
9,376,278✔
1671
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
9,378,099!
1672

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

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

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

1681
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
15,783,183!
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);
15,785,531✔
1693

1694
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
15,782,101✔
1695
  pStmt->affectedRows += pStmt->exec.affectedRows;
15,780,467✔
1696

1697
_return:
15,784,782✔
1698

1699
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
15,794,676✔
1700
    taosUsleep(1);
9,894✔
1701
  }
1702

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

1705
  tFreeSSubmitRsp(pRsp);
15,785,179✔
1706

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

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

1712
  STMT_RET(code);
15,786,723✔
1713
}
1714

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

1718
  STMT_DLOG_E("start to free stmt");
1,248,032✔
1719

1720
  if (pStmt->bindThreadInUse) {
1,248,032✔
1721
    pStmt->queue.stopQueue = true;
109,002✔
1722

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

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

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

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

1749
  return TSDB_CODE_SUCCESS;
1,251,529✔
1750
}
1751

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

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

1759
  pStmt->exec.pRequest->code = terrno;
22,606✔
1760

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

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

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

1768
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
235,337,797✔
1769
  STscStmt* pStmt = (STscStmt*)stmt;
235,337,797✔
1770

1771
  STMT_DLOG_E("start is insert");
235,337,797✔
1772

1773
  if (pStmt->sql.type) {
236,551,438✔
1774
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
227,383,850!
1775
  } else {
1776
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
9,430,720✔
1777
  }
1778

1779
  return TSDB_CODE_SUCCESS;
236,586,257✔
1780
}
1781

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

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

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

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

1797
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1798

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

1809
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1810

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

1815
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
×
1816

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

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

1829
  return code;
×
1830
}
1831

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

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

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

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

1847
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1848

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

1862
  if (pStmt->bInfo.needParse) {
×
1863
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1864
  }
1865

1866
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
×
1867

1868
_return:
×
1869

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

1875
  pStmt->errCode = preCode;
×
1876

1877
  return code;
×
1878
}
1879

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

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

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

1891
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1892

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

1903
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1904

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

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

1915
_return:
×
1916

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

1922
  pStmt->errCode = preCode;
×
1923

1924
  return code;
×
1925
}
1926

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

1932
  STMT_DLOG_E("start to get param");
×
1933

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

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

1942
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1943

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

1954
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1955

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

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

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

1971
_return:
×
1972

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

1978
  taosMemoryFree(pField);
×
1979
  pStmt->errCode = preCode;
×
1980

1981
  return code;
×
1982
}
1983

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

1987
  STMT_DLOG_E("start to use result");
26,886✔
1988

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

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