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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

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

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

49.94
/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) {
181,789!
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
181,789✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
181,789✔
UNCOV
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;
181,789✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
182,061✔
44
  int i = 0;
182,061✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
1,139,373✔
46
    if (i < 10) {
957,336✔
47
      taosUsleep(1);
878,546✔
48
      i++;
878,522✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
78,790✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
78,766!
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
78,766✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
78,790✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
182,109✔
58
    return false;
3,701✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
178,408✔
61
  SStmtQNode* node = pStmt->queue.head->next;
178,408✔
62
  pStmt->queue.head = pStmt->queue.head->next;
178,408✔
63
  *param = node;
178,408✔
64

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

67
  return true;
178,384✔
68
}
69

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

74
  pStmt->stat.bindDataNum++;
178,360✔
75

76
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
178,360✔
77
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
178,384✔
78
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
178,384✔
79
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
178,384✔
80
}
178,384✔
81

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
17,477,315✔
83
  int32_t code = 0;
17,477,315✔
84

85
  if (pStmt->exec.pRequest == NULL) {
17,477,315✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
63,810✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
63,810!
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
63,810✔
92
      pStmt->exec.pRequest->syncQuery = true;
63,314✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
63,314✔
94
    }
95
  }
96

97
  return code;
17,494,146✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
34,627,998✔
101
  int32_t code = 0;
34,627,998✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
34,627,998!
104
    STMT_LOG_SEQ(newStatus);
34,727,184✔
105
  }
106

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

112
  switch (newStatus) {
34,865,034!
113
    case STMT_PREPARE:
63,630✔
114
      pStmt->errCode = 0;
63,630✔
115
      break;
1,096✔
116
    case STMT_SETTBNAME:
90,852✔
117
      if (STMT_STATUS_EQ(INIT)) {
90,852!
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
90,852!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
90,852✔
124
    case STMT_SETTAGS:
408✔
125
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
408!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
408✔
UNCOV
129
    case STMT_FETCH_FIELDS:
×
UNCOV
130
      if (STMT_STATUS_EQ(INIT)) {
×
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
UNCOV
133
      break;
×
134
    case STMT_BIND:
17,315,815✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
17,315,815!
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;
17,342,061✔
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:
17,246,684✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
17,246,684!
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
17,343,036✔
154
    case STMT_EXECUTE:
147,645✔
155
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
147,645✔
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
464!
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)) {
147,181!
162
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
        }
164
      }
165
      break;
147,645✔
166
    default:
×
167
      code = TSDB_CODE_APP_ERROR;
×
168
      break;
×
169
  }
170

171
  STMT_ERR_RET(code);
34,872,919!
172

173
  pStmt->sql.status = newStatus;
34,872,919✔
174

175
  return TSDB_CODE_SUCCESS;
34,925,795✔
176
}
177

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

181
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
3,773✔
182

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

188
  *tbName = pStmt->bInfo.tbName;
3,773✔
189

190
  return TSDB_CODE_SUCCESS;
3,773✔
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,
62,558✔
243
                           bool autoCreateTbl, uint8_t tbNameFlag) {
244
  STscStmt* pStmt = (STscStmt*)stmt;
62,558✔
245
  char      tbFName[TSDB_TABLE_FNAME_LEN];
61,511✔
246
  int32_t   code = tNameExtractFullName(tbName, tbFName);
62,558✔
247
  if (code != 0) {
62,810!
248
    return code;
×
249
  }
250

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

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

264
  return TSDB_CODE_SUCCESS;
62,558✔
265
}
266

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

270
  pStmt->sql.pVgHash = pVgHash;
62,558✔
271
  pStmt->exec.pBlockHash = pBlockHash;
62,558✔
272

273
  return TSDB_CODE_SUCCESS;
62,810✔
274
}
275

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

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

284
  pStmt->sql.autoCreateTbl = autoCreateTbl;
62,810✔
285
  if (pStmt->sql.autoCreateTbl) {
62,558!
286
    pStmt->sql.stbInterlaceMode = false;
72✔
287
  }
288

289
  return TSDB_CODE_SUCCESS;
62,558✔
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) {
17,118,715✔
305
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
17,118,715!
306
    return TSDB_CODE_SUCCESS;
17,210,720✔
307
  }
308

UNCOV
309
  uint64_t uid = pStmt->bInfo.tbUid;
×
UNCOV
310
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
×
311

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

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

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

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

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

333
  if (pStmt->sql.autoCreateTbl) {
72!
334
    pStmt->bInfo.tagsCached = true;
72✔
335
  } else {
UNCOV
336
    pStmt->bInfo.boundTags = NULL;
×
337
  }
338

339
  return TSDB_CODE_SUCCESS;
72✔
340
}
341

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

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

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

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

359
  pStmt->bInfo.needParse = false;
63,027✔
360

361
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
63,274✔
362
    pStmt->sql.type = STMT_TYPE_INSERT;
59,037✔
363
    pStmt->sql.stbInterlaceMode = false;
59,037✔
364
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
3,990✔
365
    pStmt->sql.type = STMT_TYPE_QUERY;
464✔
366
    pStmt->sql.stbInterlaceMode = false;
464✔
367

368
    return TSDB_CODE_SUCCESS;
464✔
369
  }
370

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

377
  STableDataCxt* pTableCtx = *pSrc;
62,810✔
378
  if (pStmt->sql.stbInterlaceMode) {
62,810✔
379
    int16_t lastIdx = -1;
3,701✔
380

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

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

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

398
  return TSDB_CODE_SUCCESS;
62,563✔
399
}
400

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

409
  pStmt->bInfo.tbName[0] = 0;
215,104✔
410
  pStmt->bInfo.tbFName[0] = 0;
214,851✔
411
  if (!pStmt->bInfo.tagsCached) {
214,851!
412
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
214,479✔
413
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
214,731!
414
  }
415
  pStmt->bInfo.stbFName[0] = 0;
214,851✔
416

417
  return TSDB_CODE_SUCCESS;
214,851✔
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) {
87,844✔
426
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
87,844✔
427
  if (NULL == pTblBuf->pCurBuff) {
87,892!
UNCOV
428
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
429
    return;
×
430
  }
431
  pTblBuf->buffIdx = 1;
87,892✔
432
  pTblBuf->buffOffset = sizeof(*pQueue->head);
87,892✔
433

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

439
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
210,225✔
440
  if (pStmt->sql.stbInterlaceMode) {
210,225✔
441
    if (deepClean) {
91,593✔
442
      taosHashCleanup(pStmt->exec.pBlockHash);
3,701✔
443
      pStmt->exec.pBlockHash = NULL;
3,701✔
444

445
      if (NULL != pStmt->exec.pCurrBlock) {
3,701!
446
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
3,701!
447
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
3,701✔
448
      }
449
    } else {
450
      pStmt->sql.siInfo.pTableColsIdx = 0;
87,892✔
451
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
87,892✔
452
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
87,892✔
453
    }
454
  } else {
455
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
118,880✔
456
      taos_free_result(pStmt->exec.pRequest);
118,669✔
457
      pStmt->exec.pRequest = NULL;
118,965✔
458
    }
459

460
    size_t keyLen = 0;
119,429✔
461
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
119,682✔
462
    while (pIter) {
238,416✔
463
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
118,734✔
464
      char*          key = taosHashGetKey(pIter, &keyLen);
118,734✔
465
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
118,734✔
466

467
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
118,734✔
468
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
59,289✔
469
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
60,316!
470

471
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
58,784✔
472
        continue;
59,289✔
473
      }
474

475
      qDestroyStmtDataBlock(pBlocks);
59,445✔
476
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
59,192!
477

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

481
    if (keepTable) {
119,682✔
482
      return TSDB_CODE_SUCCESS;
59,753✔
483
    }
484

485
    taosHashCleanup(pStmt->exec.pBlockHash);
59,929✔
486
    pStmt->exec.pBlockHash = NULL;
59,676✔
487

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

492
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
151,245!
493

494
  return TSDB_CODE_SUCCESS;
151,221✔
495
}
496

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

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

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

510
  taosMemoryFree(pStmt->sql.pBindInfo);
62,625!
511
  taosMemoryFree(pStmt->sql.queryRes.fields);
63,382!
512
  taosMemoryFree(pStmt->sql.queryRes.userFields);
63,378!
513
  taosMemoryFree(pStmt->sql.sqlStr);
62,873!
514
  qDestroyQuery(pStmt->sql.pQuery);
63,377✔
515
  taosArrayDestroy(pStmt->sql.nodeList);
63,630✔
516
  taosHashCleanup(pStmt->sql.pVgHash);
63,378✔
517
  pStmt->sql.pVgHash = NULL;
63,630✔
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
63,125✔
520
  while (pIter) {
63,197✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
72✔
522

523
    qDestroyStmtDataBlock(pCache->pDataCtx);
72✔
524
    qDestroyBoundColInfo(pCache->boundTags);
72✔
525
    taosMemoryFreeClear(pCache->boundTags);
72!
526

527
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
72✔
528
  }
529
  taosHashCleanup(pStmt->sql.pTableCache);
63,125✔
530
  pStmt->sql.pTableCache = NULL;
63,377✔
531

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
63,125!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
63,125!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
63,630✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
63,377✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
63,378✔
538
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
63,377✔
539
  tSimpleHashCleanup(pStmt->sql.predicateCols);
63,125✔
540
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
63,378✔
541
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
63,125!
542
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
63,125✔
543
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
63,378✔
544

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

548
  STMT_DLOG_E("end to free SQL info");
62,877✔
549

550
  return TSDB_CODE_SUCCESS;
62,877✔
551
}
552

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

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

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

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

575
  *vgId = vgInfo.vgId;
336✔
576

577
  return TSDB_CODE_SUCCESS;
336✔
578
}
579

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

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

587
  return TSDB_CODE_SUCCESS;
336✔
588
}
589

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

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

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

UNCOV
607
  return TSDB_CODE_SUCCESS;
×
608
}
609

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

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

620
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
4,109!
621
  if (pCxtInExec) {
4,109!
UNCOV
622
    pStmt->bInfo.needParse = false;
×
UNCOV
623
    pStmt->bInfo.inExecCache = true;
×
624

UNCOV
625
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
626

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

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

638
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
4,109!
639
    if (pStmt->bInfo.inExecCache) {
3,773!
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);
3,773!
646
    return TSDB_CODE_SUCCESS;
3,773✔
647
  }
648

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

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

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

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

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

667
      return TSDB_CODE_SUCCESS;
336✔
668
    }
669

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

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

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

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

UNCOV
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

UNCOV
693
  STMT_ERR_RET(code);
×
694

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

UNCOV
701
  taosMemoryFree(pTableMeta);
×
702

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

UNCOV
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

UNCOV
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

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

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

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

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

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

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

UNCOV
757
    return TSDB_CODE_SUCCESS;
×
758
  }
759

760
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
761

762
  return TSDB_CODE_SUCCESS;
×
763
}
764

UNCOV
765
int32_t stmtResetStmt(STscStmt* pStmt) {
×
UNCOV
766
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
767

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

UNCOV
773
  if (pStmt->sql.siInfo.pTableRowDataHash) {
×
774
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
775
  }
776

UNCOV
777
  pStmt->sql.status = STMT_INIT;
×
778

UNCOV
779
  return TSDB_CODE_SUCCESS;
×
780
}
781

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

785
  if (pParam->restoreTbCols) {
178,384✔
786
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
178,360✔
787
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
90,468✔
788
      *p = taosArrayInit(20, POINTER_BYTES);
90,468✔
789
      if (*p == NULL) {
90,492!
790
        STMT_ERR_RET(terrno);
×
791
      }
792
    }
793

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

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

801
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
90,468✔
802
  }
803
  return TSDB_CODE_SUCCESS;
178,360✔
804
}
805

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

809
  qInfo("stmt bind thread started");
3,701!
810

811
  STscStmt* pStmt = (STscStmt*)param;
3,701✔
812

813
  while (true) {
182,061✔
814
    if (pStmt->queue.stopQueue) {
185,762✔
815
      break;
3,701✔
816
    }
817

818
    SStmtQNode* asyncParam = NULL;
182,061✔
819
    if (!stmtDequeue(pStmt, &asyncParam)) {
182,061✔
820
      continue;
3,701✔
821
    }
822

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

829
  qInfo("stmt bind thread stopped");
3,701!
830

831
  return NULL;
3,701✔
832
}
833

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

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

848
  pStmt->bindThreadInUse = true;
3,701✔
849

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

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

860
  return TSDB_CODE_SUCCESS;
3,701✔
861
}
862

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

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

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

883
  return TSDB_CODE_SUCCESS;
3,701✔
884
}
885

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

891
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
62,862!
892
  if (NULL == pStmt) {
63,630!
893
    return NULL;
×
894
  }
895

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

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

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

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

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

951
  pStmt->sql.siInfo.tableColsReady = true;
63,630✔
952

953
  STMT_LOG_SEQ(STMT_INIT);
63,630✔
954

955
  tscDebug("stmt:%p initialized", pStmt);
63,630✔
956

957
  return pStmt;
63,358✔
958
}
959

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

963
  STMT_DLOG_E("start to prepare");
63,630✔
964

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

969
  if (pStmt->sql.status >= STMT_PREPARE) {
63,382!
UNCOV
970
    STMT_ERR_RET(stmtResetStmt(pStmt));
×
971
  }
972

973
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
63,630!
974

975
  if (length <= 0) {
63,382✔
976
    length = strlen(sql);
59,501!
977
  }
978

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

986
  STMT_ERR_RET(stmtCreateRequest(pStmt));
63,382!
987

988
  int32_t code = 0;
63,382✔
989
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
125,717!
990
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
125,965✔
991
    // get table meta
UNCOV
992
    STableMeta* pTableMeta = NULL;
×
UNCOV
993
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
×
994

UNCOV
995
    char* newSql = NULL;
×
996

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

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

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

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

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

1027
  STMT_ERR_RET(code);
63,382!
1028

1029
  char* dbName = NULL;
63,382✔
1030
  if (qParseDbName(sql, length, &dbName)) {
63,382✔
1031
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
58,805!
1032
    taosMemoryFreeClear(dbName);
58,805!
1033
  }
1034

1035
  return TSDB_CODE_SUCCESS;
63,630✔
1036
}
1037

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

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

1048
  SArray* pTblCols = NULL;
3,701✔
1049
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
3,702,797✔
1050
    pTblCols = taosArrayInit(20, POINTER_BYTES);
3,699,096✔
1051
    if (NULL == pTblCols) {
3,695,016!
1052
      return terrno;
×
1053
    }
1054

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

1060
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
3,701✔
1061

1062
  return TSDB_CODE_SUCCESS;
3,701✔
1063
}
1064

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

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

1070
  STMT_ERR_RET(stmtCreateRequest(pStmt));
58,309!
1071

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

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

1084
  int64_t startUs = taosGetTimestampUs();
90,900✔
1085

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

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

1092
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
90,900!
1093

1094
  int32_t insert = 0;
90,876✔
1095
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
90,876!
1096
  if (0 == insert) {
90,852!
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) {
90,852✔
1102
    STMT_ERR_RET(stmtCreateRequest(pStmt));
4,085!
1103

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

1108
    STMT_ERR_RET(stmtGetFromCache(pStmt));
4,109!
1109

1110
    if (pStmt->bInfo.needParse) {
4,109!
1111
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
3,773!
1112
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
3,773✔
1113

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

1123
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
90,876✔
1124
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
3,701!
1125
  }
1126

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

1130
  return TSDB_CODE_SUCCESS;
90,900✔
1131
}
1132

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

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

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

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

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

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

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

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

UNCOV
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 =
UNCOV
1180
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
UNCOV
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

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

UNCOV
1188
  return TSDB_CODE_SUCCESS;
×
1189
}
1190

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

UNCOV
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

UNCOV
1201
  STableDataCxt** pDataBlock = NULL;
×
1202

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

UNCOV
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) {
90,492✔
1241
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
90,492✔
1242
    pStmt->sql.siInfo.pVgroupHash =
87,892✔
1243
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
87,892✔
1244
  }
1245
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
90,492✔
1246
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
87,892✔
1247
  }
1248

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

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

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

1262
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
90,492✔
1263
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
79,121!
1264
    pStmt->sql.siInfo.tbFromHash = true;
52✔
1265
  }
1266

1267
  if (0 == pStmt->sql.siInfo.firstName[0]) {
90,492✔
1268
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
3,701!
1269
  }
1270

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

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

1276
  stmtEnqueue(pStmt, param);
90,492✔
1277

1278
  return TSDB_CODE_SUCCESS;
90,492✔
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)) {
90,468!
1284
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
90,492✔
1285
      break;
90,220✔
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;
90,220✔
1302
}
1303

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

1308
  int64_t startUs = taosGetTimestampUs();
17,208,834✔
1309

1310
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
17,208,834✔
1311

1312
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
17,210,434!
1313
    return pStmt->errCode;
×
1314
  }
1315

1316
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
17,236,516!
1317

1318
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
17,307,568!
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) {
17,013,320!
1324
    taos_free_result(pStmt->exec.pRequest);
×
1325
    pStmt->exec.pRequest = NULL;
×
1326
  }
1327

1328
  STMT_ERR_RET(stmtCreateRequest(pStmt));
17,238,132!
1329

1330
  if (pStmt->bInfo.needParse) {
17,294,569✔
1331
    STMT_ERR_RET(stmtParseSql(pStmt));
59,609✔
1332
  }
1333

1334
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
17,292,155✔
1335
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
464!
1336

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

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

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

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

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

1368
    return TSDB_CODE_SUCCESS;
464✔
1369
  }
1370

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

1375
  STableDataCxt** pDataBlock = NULL;
17,266,057✔
1376

1377
  if (pStmt->exec.pCurrBlock) {
17,266,057✔
1378
    pDataBlock = &pStmt->exec.pCurrBlock;
17,271,567✔
1379
  } else {
1380
    pDataBlock =
1381
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
62,810!
1382
    if (NULL == pDataBlock) {
62,810!
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;
62,810✔
1387
    if (pStmt->sql.stbInterlaceMode) {
62,810✔
1388
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
3,701✔
1389
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
3,701✔
1390
    }
1391
  }
1392

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

1396
  SStmtQNode* param = NULL;
17,295,276✔
1397
  if (pStmt->sql.stbInterlaceMode) {
17,330,550✔
1398
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
180,936!
1399
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
180,688!
1400
    taosArrayClear(param->tblData.aCol);
90,220✔
1401

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

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

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

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

1413
  if (pStmt->sql.predicateCols) {
17,359,033!
UNCOV
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));
UNCOV
1417
    return TSDB_CODE_SUCCESS;
×
1418
  }
1419

1420
  if (colIdx < 0) {
17,307,052✔
1421
    if (pStmt->sql.stbInterlaceMode) {
17,307,020✔
1422
      (*pDataBlock)->pData->flags = 0;
90,492✔
1423
      code =
1424
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
90,492✔
1425
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
90,492✔
1426
    } else {
1427
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
17,254,431✔
1428
                                pStmt->taos->optionInfo.charsetCxt);
17,076,600✔
1429
    }
1430

1431
    if (code) {
17,162,452!
UNCOV
1432
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
×
UNCOV
1433
      STMT_ERR_RET(code);
×
1434
    }
1435
  } else {
1436
    if (pStmt->sql.stbInterlaceMode) {
32!
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) {
32!
1442
      tscError("bind column index not in sequence");
×
1443
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1444
    }
1445

1446
    pStmt->bInfo.sBindLastIdx = colIdx;
32✔
1447

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

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

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

1464
  if (pStmt->sql.stbInterlaceMode) {
17,224,489✔
1465
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
90,492!
1466
  }
1467

1468
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
17,158,065✔
1469

1470
  return TSDB_CODE_SUCCESS;
17,183,931✔
1471
}
1472

1473
int stmtAddBatch(TAOS_STMT* stmt) {
17,202,736✔
1474
  STscStmt* pStmt = (STscStmt*)stmt;
17,202,736✔
1475

1476
  int64_t startUs = taosGetTimestampUs();
17,298,418✔
1477

1478
  STMT_DLOG_E("start to add batch");
17,298,418!
1479

1480
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
17,298,354!
1481
    return pStmt->errCode;
×
1482
  }
1483

1484
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
17,209,858!
1485

1486
  if (pStmt->sql.stbInterlaceMode) {
17,305,718✔
1487
    int64_t startUs2 = taosGetTimestampUs();
87,892✔
1488
    pStmt->stat.addBatchUs += startUs2 - startUs;
87,892✔
1489

1490
    pStmt->sql.siInfo.tableColsReady = false;
87,892✔
1491

1492
    SStmtQNode* param = NULL;
87,892✔
1493
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
175,784!
1494
    param->restoreTbCols = true;
87,892✔
1495
    param->next = NULL;
87,892✔
1496

1497
    stmtEnqueue(pStmt, param);
87,892✔
1498

1499
    return TSDB_CODE_SUCCESS;
87,892✔
1500
  }
1501

1502
  STMT_ERR_RET(stmtCacheBlock(pStmt));
17,161,789✔
1503

1504
  return TSDB_CODE_SUCCESS;
17,194,757✔
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) {
147,621✔
1641
  STscStmt*   pStmt = (STscStmt*)stmt;
147,621✔
1642
  int32_t     code = 0;
147,621✔
1643
  SSubmitRsp* pRsp = NULL;
147,621✔
1644

1645
  int64_t startUs = taosGetTimestampUs();
147,645✔
1646

1647
  STMT_DLOG_E("start to exec");
147,645!
1648

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

1653
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
147,645!
1654

1655
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
147,645✔
1656
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
464✔
1657
  } else {
1658
    if (pStmt->sql.stbInterlaceMode) {
147,181✔
1659
      int64_t startTs = taosGetTimestampUs();
87,892✔
1660
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
421,382✔
1661
        taosUsleep(1);
333,762✔
1662
      }
1663
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
87,868✔
1664

1665
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
87,868!
1666
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
87,892✔
1667
      pStmt->sql.siInfo.pVgroupHash = NULL;
87,892✔
1668
      pStmt->sql.siInfo.pVgroupList = NULL;
87,892✔
1669
    } else {
1670
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
59,289✔
1671
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
59,289!
1672

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

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

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

1681
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
147,096!
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);
147,397!
1693

1694
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
146,896✔
1695
  pStmt->affectedRows += pStmt->exec.affectedRows;
146,644✔
1696

1697
_return:
147,145✔
1698

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

1703
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
147,645!
1704

1705
  tFreeSSubmitRsp(pRsp);
147,597✔
1706

1707
  ++pStmt->sql.runTimes;
147,597✔
1708

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

1712
  STMT_RET(code);
147,597!
1713
}
1714

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

1718
  STMT_DLOG_E("start to free stmt");
62,877✔
1719

1720
  if (pStmt->bindThreadInUse) {
62,877✔
1721
    pStmt->queue.stopQueue = true;
3,701✔
1722

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

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

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

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

1749
  return TSDB_CODE_SUCCESS;
63,130✔
1750
}
1751

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

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

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

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

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

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

1768
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
17,420,397✔
1769
  STscStmt* pStmt = (STscStmt*)stmt;
17,420,397✔
1770

1771
  STMT_DLOG_E("start is insert");
17,420,397!
1772

1773
  if (pStmt->sql.type) {
17,421,325✔
1774
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
17,406,525!
1775
  } else {
1776
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
62,578✔
1777
  }
1778

1779
  return TSDB_CODE_SUCCESS;
17,433,356✔
1780
}
1781

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

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

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

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

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

UNCOV
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

UNCOV
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

UNCOV
1809
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1810

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

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

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

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

UNCOV
1829
  return code;
×
1830
}
1831

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

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

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

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

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

UNCOV
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

UNCOV
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

UNCOV
1860
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1861

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

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

UNCOV
1868
_return:
×
1869

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

UNCOV
1875
  pStmt->errCode = preCode;
×
1876

UNCOV
1877
  return code;
×
1878
}
1879

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

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

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

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

UNCOV
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

UNCOV
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

UNCOV
1903
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1904

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

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

UNCOV
1915
_return:
×
1916

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

UNCOV
1922
  pStmt->errCode = preCode;
×
1923

UNCOV
1924
  return code;
×
1925
}
1926

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

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

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

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

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

UNCOV
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

UNCOV
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

UNCOV
1954
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1955

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

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

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

UNCOV
1971
_return:
×
1972

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

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

UNCOV
1981
  return code;
×
1982
}
1983

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

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

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

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