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

taosdata / TDengine / #4909

30 Dec 2025 10:52AM UTC coverage: 65.542% (+0.2%) from 65.386%
#4909

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

857 existing lines in 113 files now uncovered.

193924 of 295877 relevant lines covered (65.54%)

120594206.86 hits per line

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

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

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

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

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

13
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
14
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
2,618,716✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
2,619,608✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
2,619,178✔
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;
2,614,316✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
2,618,054✔
44
  int i = 0;
2,618,054✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
8,221,811✔
46
    if (i < 10) {
5,603,206✔
47
      taosUsleep(1);
5,257,213✔
48
      i++;
5,257,811✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
345,993✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
346,052✔
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
346,052✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
345,850✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
2,618,197✔
58
    return false;
9,167✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
2,609,324✔
61
  SStmtQNode* node = pStmt->queue.head->next;
2,609,668✔
62
  pStmt->queue.head = pStmt->queue.head->next;
2,609,834✔
63
  *param = node;
2,609,609✔
64

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

67
  return true;
2,611,052✔
68
}
69

70
void stmtEnqueue(STscStmt* pStmt, SStmtQNode* param) {
2,608,957✔
71
  pStmt->queue.tail->next = param;
2,608,957✔
72
  pStmt->queue.tail = param;
2,609,548✔
73

74
  pStmt->stat.bindDataNum++;
2,609,018✔
75

76
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,608,517✔
77
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
2,610,189✔
78
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
2,611,312✔
79
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,607,796✔
80
}
2,609,741✔
81

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
71,325,158✔
83
  int32_t code = 0;
71,325,158✔
84

85
  if (pStmt->exec.pRequest == NULL) {
71,325,158✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
3,375,633✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
3,375,310✔
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
3,375,310✔
92
      pStmt->exec.pRequest->syncQuery = true;
3,375,310✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
3,374,987✔
94
    }
95
  }
96

97
  return code;
70,830,205✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
130,018,063✔
101
  int32_t code = 0;
130,018,063✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
130,018,063✔
104
    STMT_LOG_SEQ(newStatus);
131,665,612✔
105
  }
106

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

112
  switch (newStatus) {
132,172,371✔
113
    case STMT_PREPARE:
3,361,474✔
114
      pStmt->errCode = 0;
3,361,474✔
115
      break;
2,749,617✔
116
    case STMT_SETTBNAME:
5,504,054✔
117
      if (STMT_STATUS_EQ(INIT)) {
5,504,054✔
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
5,505,232✔
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
5,503,162✔
124
    case STMT_SETTAGS:
5,435✔
125
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
5,435✔
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
5,435✔
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:
60,728,146✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
60,728,146✔
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;
61,282,066✔
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:
58,874,161✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
58,874,161✔
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
59,523,261✔
154
    case STMT_EXECUTE:
3,699,101✔
155
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
3,699,101✔
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
9,069✔
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)) {
3,688,321✔
162
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
        }
164
      }
165
      break;
3,697,809✔
166
    default:
×
167
      code = TSDB_CODE_APP_ERROR;
×
168
      break;
×
169
  }
170

171
  STMT_ERR_RET(code);
132,083,653✔
172

173
  pStmt->sql.status = newStatus;
132,083,653✔
174

175
  return TSDB_CODE_SUCCESS;
132,612,477✔
176
}
177

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

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

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

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

190
  return TSDB_CODE_SUCCESS;
3,240,222✔
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,
3,344,275✔
243
                           bool autoCreateTbl, uint8_t tbNameFlag) {
244
  STscStmt* pStmt = (STscStmt*)stmt;
3,344,275✔
245
  char      tbFName[TSDB_TABLE_FNAME_LEN];
3,334,172✔
246
  int32_t   code = tNameExtractFullName(tbName, tbFName);
3,346,095✔
247
  if (code != 0) {
3,344,250✔
248
    qDestroyBoundColInfo(tags);
×
249
    taosMemoryFreeClear(tags);
×
250
    return code;
×
251
  }
252

253
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
3,344,250✔
254
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
3,344,250✔
255
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
3,344,573✔
256

257
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
3,344,573✔
258
  pStmt->bInfo.tbSuid = pTableMeta->suid;
3,346,070✔
259
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
3,343,927✔
260
  pStmt->bInfo.tbType = pTableMeta->tableType;
3,343,545✔
261
  if (pStmt->bInfo.boundTags && !pStmt->bInfo.tagsCached) {
3,343,222✔
262
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
×
263
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
×
264
  }
265
  pStmt->bInfo.boundTags = tags;
3,342,253✔
266
  pStmt->bInfo.tagsCached = false;
3,345,542✔
267
  pStmt->bInfo.tbNameFlag = tbNameFlag;
3,344,573✔
268
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
3,343,927✔
269

270
  return TSDB_CODE_SUCCESS;
3,343,809✔
271
}
272

273
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
3,343,281✔
274
  STscStmt* pStmt = (STscStmt*)stmt;
3,343,281✔
275

276
  pStmt->sql.pVgHash = pVgHash;
3,343,281✔
277
  pStmt->exec.pBlockHash = pBlockHash;
3,347,039✔
278

279
  return TSDB_CODE_SUCCESS;
3,343,927✔
280
}
281

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

287
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
3,344,289✔
288
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
3,341,666✔
289

290
  pStmt->sql.autoCreateTbl = autoCreateTbl;
3,341,975✔
291
  if (pStmt->sql.autoCreateTbl) {
3,342,885✔
292
    pStmt->sql.stbInterlaceMode = false;
3,229,311✔
293
  }
294

295
  return TSDB_CODE_SUCCESS;
3,339,655✔
296
}
297

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

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

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

307
  return TSDB_CODE_SUCCESS;
×
308
}
309

310
int32_t stmtCacheBlock(STscStmt* pStmt) {
57,705,485✔
311
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
57,705,485✔
312
    return TSDB_CODE_SUCCESS;
55,218,740✔
313
  }
314

315
  uint64_t uid = pStmt->bInfo.tbUid;
3,241,214✔
316
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
3,241,214✔
317

318
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
3,241,537✔
319
    return TSDB_CODE_SUCCESS;
8,158✔
320
  }
321

322
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,234,802✔
323
  if (!pSrc) {
3,234,479✔
324
    return terrno;
×
325
  }
326
  STableDataCxt* pDst = NULL;
3,234,479✔
327

328
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3,235,125✔
329

330
  SStmtTableCache cache = {
3,234,156✔
331
      .pDataCtx = pDst,
332
      .boundTags = pStmt->bInfo.boundTags,
3,234,156✔
333
  };
334

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

339
  if (pStmt->sql.autoCreateTbl) {
3,234,156✔
340
    pStmt->bInfo.tagsCached = true;
3,234,479✔
341
  } else {
UNCOV
342
    pStmt->bInfo.boundTags = NULL;
×
343
  }
344

345
  return TSDB_CODE_SUCCESS;
3,232,541✔
346
}
347

348
int32_t stmtParseSql(STscStmt* pStmt) {
3,357,521✔
349
  pStmt->exec.pCurrBlock = NULL;
3,357,521✔
350

351
  SStmtCallback stmtCb = {
3,359,136✔
352
      .pStmt = pStmt,
353
      .getTbNameFn = stmtGetTbName,
354
      .setInfoFn = stmtUpdateInfo,
355
      .getExecInfoFn = stmtGetExecInfo,
356
  };
357

358
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3,357,844✔
359
  pStmt->exec.pRequest->stmtBindVersion = 1;
3,356,552✔
360

361
  pStmt->stat.parseSqlNum++;
3,357,198✔
362
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
3,358,490✔
363
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
3,353,358✔
364

365
  pStmt->bInfo.needParse = false;
3,353,035✔
366

367
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
3,352,389✔
368
    pStmt->sql.type = STMT_TYPE_INSERT;
107,861✔
369
    pStmt->sql.stbInterlaceMode = false;
107,861✔
370
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
3,241,826✔
371
    pStmt->sql.type = STMT_TYPE_QUERY;
9,069✔
372
    pStmt->sql.stbInterlaceMode = false;
9,069✔
373

374
    return TSDB_CODE_SUCCESS;
9,069✔
375
  }
376

377
  STableDataCxt** pSrc =
378
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,346,227✔
379
  if (NULL == pSrc || NULL == *pSrc) {
3,348,047✔
UNCOV
380
    return terrno;
×
381
  }
382

383
  STableDataCxt* pTableCtx = *pSrc;
3,346,432✔
384
  if (pStmt->sql.stbInterlaceMode) {
3,346,755✔
385
    int16_t lastIdx = -1;
5,825✔
386

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

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

397
  if (NULL == pStmt->sql.pBindInfo) {
3,346,227✔
398
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
3,344,289✔
399
    if (NULL == pStmt->sql.pBindInfo) {
3,344,612✔
400
      return terrno;
×
401
    }
402
  }
403

404
  return TSDB_CODE_SUCCESS;
3,341,705✔
405
}
406

407
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
7,061,810✔
408
  pStmt->bInfo.tbUid = 0;
7,061,810✔
409
  pStmt->bInfo.tbSuid = 0;
7,062,601✔
410
  pStmt->bInfo.tbVgId = -1;
7,062,933✔
411
  pStmt->bInfo.tbType = 0;
7,061,732✔
412
  pStmt->bInfo.needParse = true;
7,062,523✔
413
  pStmt->bInfo.inExecCache = false;
7,062,516✔
414

415
  pStmt->bInfo.tbName[0] = 0;
7,062,098✔
416
  pStmt->bInfo.tbFName[0] = 0;
7,063,257✔
417
  if (!pStmt->bInfo.tagsCached) {
7,062,280✔
418
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
592,663✔
419
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
592,792✔
420
  }
421
  pStmt->bInfo.stbFName[0] = 0;
7,061,156✔
422

423
  return TSDB_CODE_SUCCESS;
7,061,052✔
424
}
425

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

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

440
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
337,692✔
441
  pQueue->qRemainNum = 0;
337,548✔
442
  pQueue->head->next = NULL;
337,644✔
443
}
444

445
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
7,059,770✔
446
  if (pStmt->sql.stbInterlaceMode) {
7,059,770✔
447
    if (deepClean) {
343,469✔
448
      taosHashCleanup(pStmt->exec.pBlockHash);
5,825✔
449
      pStmt->exec.pBlockHash = NULL;
5,825✔
450

451
      if (NULL != pStmt->exec.pCurrBlock) {
5,825✔
452
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
5,825✔
453
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
5,825✔
454
      }
455
    } else {
456
      pStmt->sql.siInfo.pTableColsIdx = 0;
337,644✔
457
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
337,596✔
458
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
337,692✔
459
    }
460
  } else {
461
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
6,717,223✔
462
      taos_free_result(pStmt->exec.pRequest);
6,709,315✔
463
      pStmt->exec.pRequest = NULL;
6,704,470✔
464
    }
465

466
    size_t keyLen = 0;
6,716,446✔
467
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
6,716,446✔
468
    while (pIter) {
13,413,919✔
469
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
6,695,535✔
470
      char*          key = taosHashGetKey(pIter, &keyLen);
6,696,181✔
471
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
6,695,212✔
472

473
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
6,695,858✔
474
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
3,352,456✔
475
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
3,361,955✔
476

477
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
3,350,154✔
478
        continue;
3,352,133✔
479
      }
480

481
      qDestroyStmtDataBlock(pBlocks);
3,343,402✔
482
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
3,342,756✔
483

484
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
3,341,787✔
485
    }
486

487
    if (keepTable) {
6,718,384✔
488
      return TSDB_CODE_SUCCESS;
3,361,202✔
489
    }
490

491
    taosHashCleanup(pStmt->exec.pBlockHash);
3,357,182✔
492
    pStmt->exec.pBlockHash = NULL;
3,357,505✔
493

494
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
3,357,505✔
495
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
3,357,505✔
496
  }
497

498
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,700,380✔
499

500
  return TSDB_CODE_SUCCESS;
3,698,657✔
501
}
502

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

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

513
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
3,361,151✔
514
  STMT_DLOG_E("start to free SQL info");
3,361,151✔
515

516
  taosMemoryFree(pStmt->sql.pBindInfo);
3,361,151✔
517
  taosMemoryFree(pStmt->sql.queryRes.fields);
3,360,828✔
518
  taosMemoryFree(pStmt->sql.queryRes.userFields);
3,360,505✔
519
  taosMemoryFree(pStmt->sql.sqlStr);
3,360,787✔
520
  qDestroyQuery(pStmt->sql.pQuery);
3,359,821✔
521
  taosArrayDestroy(pStmt->sql.nodeList);
3,361,474✔
522
  taosHashCleanup(pStmt->sql.pVgHash);
3,361,433✔
523
  pStmt->sql.pVgHash = NULL;
3,361,474✔
524

525
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
3,362,443✔
526
  while (pIter) {
6,596,922✔
527
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
3,234,802✔
528

529
    qDestroyStmtDataBlock(pCache->pDataCtx);
3,234,802✔
530
    qDestroyBoundColInfo(pCache->boundTags);
3,234,479✔
531
    taosMemoryFreeClear(pCache->boundTags);
3,234,802✔
532

533
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
3,234,479✔
534
  }
535
  taosHashCleanup(pStmt->sql.pTableCache);
3,362,120✔
536
  pStmt->sql.pTableCache = NULL;
3,362,766✔
537

538
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
3,362,766✔
539
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,361,396✔
540

541
  taos_free_result(pStmt->sql.siInfo.pRequest);
3,362,766✔
542
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
3,362,405✔
543
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
3,362,405✔
544
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
3,361,436✔
545
  tSimpleHashCleanup(pStmt->sql.predicateCols);
3,362,405✔
546
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
3,361,436✔
547
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
3,361,436✔
548
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
3,362,120✔
549
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
3,361,757✔
550

551
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
3,360,505✔
552
  pStmt->sql.siInfo.tableColsReady = true;
3,361,474✔
553

554
  STMT_DLOG_E("end to free SQL info");
3,360,104✔
555

556
  return TSDB_CODE_SUCCESS;
3,360,790✔
557
}
558

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

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

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

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

581
  *vgId = vgInfo.vgId;
534✔
582

583
  return TSDB_CODE_SUCCESS;
534✔
584
}
585

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

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

593
  return TSDB_CODE_SUCCESS;
534✔
594
}
595

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

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

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

613
  return TSDB_CODE_SUCCESS;
×
614
}
615

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

623
  pStmt->bInfo.needParse = true;
3,240,980✔
624
  pStmt->bInfo.inExecCache = false;
3,240,011✔
625

626
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,240,011✔
627
  if (pCxtInExec) {
3,241,949✔
628
    pStmt->bInfo.needParse = false;
224✔
629
    pStmt->bInfo.inExecCache = true;
224✔
630

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

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

639
  if (NULL == pStmt->pCatalog) {
3,241,725✔
640
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
14,632✔
641
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
14,632✔
642
  }
643

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

651
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
3,241,191✔
652
    return TSDB_CODE_SUCCESS;
3,238,607✔
653
  }
654

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

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

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

669
      pStmt->exec.pCurrBlock = pNewBlock;
534✔
670

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

673
      return TSDB_CODE_SUCCESS;
534✔
674
    }
675

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

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

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

690
  pStmt->stat.ctgGetTbMetaNum++;
×
691

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

696
    STMT_ERR_RET(code);
×
697
  }
698

699
  STMT_ERR_RET(code);
×
700

701
  uid = pTableMeta->uid;
×
702
  suid = pTableMeta->suid;
×
703
  tableType = pTableMeta->tableType;
×
704
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
705
  vgId = pTableMeta->vgId;
×
706

707
  taosMemoryFree(pTableMeta);
×
708

709
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
710

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

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

716
    return TSDB_CODE_SUCCESS;
×
717
  }
718

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

725
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
726
    }
727

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

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

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

738
    return TSDB_CODE_SUCCESS;
×
739
  }
740

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

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

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

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

759
    pStmt->exec.pCurrBlock = pNewBlock;
×
760

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

763
    return TSDB_CODE_SUCCESS;
×
764
  }
765

766
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
767

768
  return TSDB_CODE_SUCCESS;
×
769
}
770

771
int32_t stmtResetStmt(STscStmt* pStmt) {
3,225,267✔
772
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
3,225,267✔
773

774
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
3,224,944✔
775
  if (NULL == pStmt->sql.pTableCache) {
3,225,590✔
776
    STMT_ERR_RET(terrno);
×
777
  }
778

779
  if (pStmt->sql.siInfo.pTableRowDataHash) {
3,225,590✔
780
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
781
  }
782

783
  pStmt->sql.status = STMT_INIT;
3,226,236✔
784

785
  return TSDB_CODE_SUCCESS;
3,225,913✔
786
}
787

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

791
  if (pParam->restoreTbCols) {
2,608,843✔
792
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
2,611,132✔
793
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
2,273,437✔
794
      *p = taosArrayInit(20, POINTER_BYTES);
2,273,567✔
795
      if (*p == NULL) {
2,273,388✔
796
        STMT_ERR_RET(terrno);
×
797
      }
798
    }
799

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

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

807
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
2,270,732✔
808
  }
809
  return TSDB_CODE_SUCCESS;
2,611,240✔
810
}
811

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

815
  qInfo("stmt bind thread started");
9,167✔
816

817
  STscStmt* pStmt = (STscStmt*)param;
9,167✔
818

819
  while (true) {
2,620,016✔
820
    if (pStmt->queue.stopQueue) {
2,629,183✔
821
      break;
9,167✔
822
    }
823

824
    SStmtQNode* asyncParam = NULL;
2,619,566✔
825
    if (!stmtDequeue(pStmt, &asyncParam)) {
2,619,838✔
826
      continue;
9,167✔
827
    }
828

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

835
  qInfo("stmt bind thread stopped");
9,392✔
836

837
  return NULL;
9,167✔
838
}
839

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

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

854
  pStmt->bindThreadInUse = true;
9,167✔
855

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

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

866
  return TSDB_CODE_SUCCESS;
9,167✔
867
}
868

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

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

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

889
  return TSDB_CODE_SUCCESS;
9,167✔
890
}
891

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

897
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
135,779✔
898
  if (NULL == pStmt) {
135,884✔
899
    return NULL;
×
900
  }
901

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

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

914
  if (NULL != pOptions) {
135,884✔
915
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
9,167✔
916
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
9,167✔
917
      pStmt->stbInterlaceMode = true;
9,167✔
918
    }
919
  }
920

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

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

957
  pStmt->sql.siInfo.tableColsReady = true;
135,884✔
958

959
  STMT_LOG_SEQ(STMT_INIT);
135,884✔
960

961
  tscDebug("stmt:%p initialized", pStmt);
135,884✔
962

963
  return pStmt;
135,884✔
964
}
965

966
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
3,361,151✔
967
  STscStmt* pStmt = (STscStmt*)stmt;
3,361,151✔
968

969
  STMT_DLOG_E("start to prepare");
3,361,151✔
970

971
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
3,361,151✔
972
    return pStmt->errCode;
×
973
  }
974

975
  if (pStmt->sql.status >= STMT_PREPARE) {
3,362,120✔
976
    STMT_ERR_RET(stmtResetStmt(pStmt));
3,226,559✔
977
  }
978

979
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
3,361,474✔
980

981
  if (length <= 0) {
3,361,797✔
982
    length = strlen(sql);
107,907✔
983
  }
984

985
  pStmt->sql.sqlStr = taosStrndup(sql, length);
3,361,797✔
986
  if (!pStmt->sql.sqlStr) {
3,362,766✔
987
    return terrno;
×
988
  }
989
  pStmt->sql.sqlLen = length;
3,361,151✔
990
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
3,360,505✔
991

992
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3,361,474✔
993

994
  int32_t code = 0;
3,361,151✔
995
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
6,711,862✔
996
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
6,713,154✔
997
    // get table meta
998
    STableMeta* pTableMeta = NULL;
×
999
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
×
1000

1001
    char* newSql = NULL;
×
1002

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

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

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

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

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

1033
  STMT_ERR_RET(code);
3,360,505✔
1034

1035
  char* dbName = NULL;
3,360,505✔
1036
  if (qParseDbName(sql, length, &dbName)) {
3,361,474✔
1037
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
107,899✔
1038
    taosMemoryFreeClear(dbName);
107,576✔
1039
  }
1040

1041
  return TSDB_CODE_SUCCESS;
3,361,151✔
1042
}
1043

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

1051
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
5,825✔
1052
  pStmt->sql.siInfo.pDataCtx = pDst;
5,766✔
1053

1054
  SArray* pTblCols = NULL;
5,766✔
1055
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
5,806,308✔
1056
    pTblCols = taosArrayInit(20, POINTER_BYTES);
5,800,847✔
1057
    if (NULL == pTblCols) {
5,795,430✔
1058
      return terrno;
×
1059
    }
1060

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

1066
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
5,503✔
1067

1068
  return TSDB_CODE_SUCCESS;
5,503✔
1069
}
1070

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

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

1076
  STMT_ERR_RET(stmtCreateRequest(pStmt));
107,899✔
1077

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

1087
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
5,501,220✔
1088
  STscStmt* pStmt = (STscStmt*)stmt;
5,501,220✔
1089

1090
  int64_t startUs = taosGetTimestampUs();
5,507,498✔
1091

1092
  STMT_DLOG("start to set tbName:%s", tbName);
5,507,498✔
1093

1094
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
5,507,498✔
1095
    return pStmt->errCode;
×
1096
  }
1097

1098
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
5,508,172✔
1099

1100
  int32_t insert = 0;
5,506,801✔
1101
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
5,502,854✔
1102
  if (0 == insert) {
5,505,515✔
1103
    tscError("set tb name not available for none insert statement");
×
1104
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1105
  }
1106

1107
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
5,505,515✔
1108
    STMT_ERR_RET(stmtCreateRequest(pStmt));
3,239,391✔
1109

1110
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
3,241,626✔
1111
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1112
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
3,240,334✔
1113

1114
    STMT_ERR_RET(stmtGetFromCache(pStmt));
3,240,334✔
1115

1116
    if (pStmt->bInfo.needParse) {
3,240,657✔
1117
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
3,239,899✔
1118
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
3,238,930✔
1119

1120
      STMT_ERR_RET(stmtParseSql(pStmt));
3,239,576✔
1121
    }
1122
  } else {
1123
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
2,263,473✔
1124
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
2,264,146✔
1125
    pStmt->exec.pRequest->requestId++;
2,264,124✔
1126
    pStmt->bInfo.needParse = false;
2,266,344✔
1127
  }
1128

1129
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
5,501,972✔
1130
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
5,825✔
1131
  }
1132

1133
  int64_t startUs2 = taosGetTimestampUs();
5,502,497✔
1134
  pStmt->stat.setTbNameUs += startUs2 - startUs;
5,502,497✔
1135

1136
  return TSDB_CODE_SUCCESS;
5,504,903✔
1137
}
1138

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

1142
  STMT_DLOG_E("start to set tbTags");
5,435✔
1143

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

1148
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
5,435✔
1149

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

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

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

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

1172
  return TSDB_CODE_SUCCESS;
5,435✔
1173
}
1174

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

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

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

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

1194
  return TSDB_CODE_SUCCESS;
×
1195
}
1196

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

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

1207
  STableDataCxt** pDataBlock = NULL;
×
1208

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

1225
  return TSDB_CODE_SUCCESS;
×
1226
}
1227

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

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

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

1246
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
2,268,664✔
1247
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
2,268,664✔
1248
    pStmt->sql.siInfo.pVgroupHash =
337,707✔
1249
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
337,648✔
1250
  }
1251
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
2,271,075✔
1252
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
337,803✔
1253
  }
1254

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

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

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

1268
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
2,271,341✔
1269
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
46,096✔
1270
    pStmt->sql.siInfo.tbFromHash = true;
1,322✔
1271
  }
1272

1273
  if (0 == pStmt->sql.siInfo.firstName[0]) {
2,270,841✔
1274
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,779✔
1275
  }
1276

1277
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
2,271,360✔
1278
  param->next = NULL;
2,270,961✔
1279

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

1282
  stmtEnqueue(pStmt, param);
2,272,483✔
1283

1284
  return TSDB_CODE_SUCCESS;
2,272,341✔
1285
}
1286

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

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

1307
  return TSDB_CODE_SUCCESS;
2,269,467✔
1308
}
1309

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

1314
  int64_t startUs = taosGetTimestampUs();
61,013,658✔
1315

1316
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
61,013,658✔
1317

1318
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
60,948,216✔
1319
    return pStmt->errCode;
×
1320
  }
1321

1322
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
61,371,907✔
1323

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

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

1334
  STMT_ERR_RET(stmtCreateRequest(pStmt));
61,133,910✔
1335

1336
  if (pStmt->bInfo.needParse) {
60,351,871✔
1337
    STMT_ERR_RET(stmtParseSql(pStmt));
118,268✔
1338
  }
1339

1340
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
60,608,126✔
1341
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
9,069✔
1342

1343
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
9,069✔
1344
                         .acctId = pStmt->taos->acctId,
9,069✔
1345
                         .db = pStmt->exec.pRequest->pDb,
9,069✔
1346
                         .topicQuery = false,
1347
                         .pSql = pStmt->sql.sqlStr,
9,069✔
1348
                         .sqlLen = pStmt->sql.sqlLen,
9,069✔
1349
                         .pMsg = pStmt->exec.pRequest->msgBuf,
9,069✔
1350
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1351
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
9,069✔
1352
                         .pStmtCb = NULL,
1353
                         .pUser = pStmt->taos->user,
9,069✔
1354
                         .setQueryFp = setQueryRequest,
1355
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
9,069✔
1356

1357
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
9,069✔
1358
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
9,069✔
1359

1360
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
9,069✔
1361

1362
    if (pStmt->sql.pQuery->haveResultSet) {
9,069✔
1363
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
9,069✔
1364
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1365
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
9,069✔
1366
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
9,069✔
1367
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
9,069✔
1368
    }
1369

1370
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
9,069✔
1371
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
9,069✔
1372
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
9,069✔
1373

1374
    return TSDB_CODE_SUCCESS;
9,069✔
1375
  }
1376

1377
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
61,011,346✔
1378
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1379
  }
1380

1381
  STableDataCxt** pDataBlock = NULL;
60,330,391✔
1382

1383
  if (pStmt->exec.pCurrBlock) {
60,330,391✔
1384
    pDataBlock = &pStmt->exec.pCurrBlock;
57,880,607✔
1385
  } else {
1386
    pDataBlock =
1387
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,345,699✔
1388
    if (NULL == pDataBlock) {
3,347,724✔
1389
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1390
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1391
    }
1392
    pStmt->exec.pCurrBlock = *pDataBlock;
3,347,724✔
1393
    if (pStmt->sql.stbInterlaceMode) {
3,347,078✔
1394
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
5,825✔
1395
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
5,825✔
1396
    }
1397
  }
1398

1399
  int64_t startUs2 = taosGetTimestampUs();
60,940,717✔
1400
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
60,940,717✔
1401

1402
  SStmtQNode* param = NULL;
61,472,756✔
1403
  if (pStmt->sql.stbInterlaceMode) {
61,596,938✔
1404
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
4,537,876✔
1405
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
4,536,286✔
1406
    taosArrayClear(param->tblData.aCol);
2,269,467✔
1407

1408
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1409

1410
    param->restoreTbCols = false;
2,268,757✔
1411
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
2,269,325✔
1412
  }
1413

1414
  int64_t startUs3 = taosGetTimestampUs();
60,960,951✔
1415
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
60,960,951✔
1416

1417
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
60,954,117✔
1418

1419
  if (pStmt->sql.predicateCols) {
61,622,862✔
1420
    STMT_ERR_RET(qBindUpdateStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1421
                                          pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt,
1422
                                          pStmt->sql.predicateCols));
1423
    return TSDB_CODE_SUCCESS;
×
1424
  }
1425

1426
  if (colIdx < 0) {
61,331,466✔
1427
    if (pStmt->sql.stbInterlaceMode) {
61,328,254✔
1428
      (*pDataBlock)->pData->flags = 0;
2,270,107✔
1429
      code =
1430
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
2,272,383✔
1431
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
2,272,387✔
1432
    } else {
1433
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
59,135,662✔
1434
                                pStmt->taos->optionInfo.charsetCxt);
59,115,851✔
1435
    }
1436

1437
    if (code) {
60,497,246✔
1438
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
3,684✔
1439
      STMT_ERR_RET(code);
3,684✔
1440
    }
1441
  } else {
1442
    if (pStmt->sql.stbInterlaceMode) {
3,212✔
1443
      tscError("bind single column not allowed in stb insert mode");
×
1444
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1445
    }
1446

1447
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
3,212✔
1448
      tscError("bind column index not in sequence");
×
1449
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1450
    }
1451

1452
    pStmt->bInfo.sBindLastIdx = colIdx;
3,212✔
1453

1454
    if (0 == colIdx) {
3,212✔
1455
      pStmt->bInfo.sBindRowNum = bind->num;
240✔
1456
    }
1457

1458
    code =
1459
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
3,212✔
1460
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
3,212✔
1461
    if (code) {
1,120✔
1462
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1463
      STMT_ERR_RET(code);
×
1464
    }
1465
  }
1466

1467
  int64_t startUs4 = taosGetTimestampUs();
60,983,085✔
1468
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
60,983,085✔
1469

1470
  if (pStmt->sql.stbInterlaceMode) {
61,484,507✔
1471
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
2,271,815✔
1472
  }
1473

1474
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
61,488,541✔
1475

1476
  return TSDB_CODE_SUCCESS;
61,738,136✔
1477
}
1478

1479
int stmtAddBatch(TAOS_STMT* stmt) {
58,013,454✔
1480
  STscStmt* pStmt = (STscStmt*)stmt;
58,013,454✔
1481

1482
  int64_t startUs = taosGetTimestampUs();
59,058,945✔
1483

1484
  STMT_DLOG_E("start to add batch");
59,058,945✔
1485

1486
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
59,088,078✔
1487
    return pStmt->errCode;
×
1488
  }
1489

1490
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
59,021,840✔
1491

1492
  if (pStmt->sql.stbInterlaceMode) {
59,405,804✔
1493
    int64_t startUs2 = taosGetTimestampUs();
337,753✔
1494
    pStmt->stat.addBatchUs += startUs2 - startUs;
337,753✔
1495

1496
    pStmt->sql.siInfo.tableColsReady = false;
337,514✔
1497

1498
    SStmtQNode* param = NULL;
337,466✔
1499
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
674,977✔
1500
    param->restoreTbCols = true;
337,465✔
1501
    param->next = NULL;
337,564✔
1502

1503
    stmtEnqueue(pStmt, param);
337,705✔
1504

1505
    return TSDB_CODE_SUCCESS;
337,803✔
1506
  }
1507

1508
  STMT_ERR_RET(stmtCacheBlock(pStmt));
58,711,888✔
1509

1510
  return TSDB_CODE_SUCCESS;
58,605,028✔
1511
}
1512
/*
1513
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1514
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1515

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

1524
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1525
    if (pMeta->uid) {
1526
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1527
      continue;
1528
    }
1529

1530
    SSubmitBlkRsp* blkRsp = NULL;
1531
    int32_t        i = 0;
1532
    for (; i < pRsp->nBlocks; ++i) {
1533
      blkRsp = pRsp->pBlocks + i;
1534
      if (strlen(blkRsp->tblFName) != keyLen) {
1535
        continue;
1536
      }
1537

1538
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1539
        continue;
1540
      }
1541

1542
      break;
1543
    }
1544

1545
    if (i < pRsp->nBlocks) {
1546
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1547
               blkRsp->uid);
1548

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

1562
      code = stmtCreateRequest(pStmt);
1563
      if (code) {
1564
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1565
        finalCode = code;
1566
        continue;
1567
      }
1568

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

1576
      pStmt->stat.ctgGetTbMetaNum++;
1577

1578
      taos_free_result(pStmt->exec.pRequest);
1579
      pStmt->exec.pRequest = NULL;
1580

1581
      if (code || NULL == pTableMeta) {
1582
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1583
        finalCode = code;
1584
        taosMemoryFree(pTableMeta);
1585
        continue;
1586
      }
1587

1588
      pMeta->uid = pTableMeta->uid;
1589
      pStmt->bInfo.tbUid = pTableMeta->uid;
1590
      taosMemoryFree(pTableMeta);
1591
    }
1592

1593
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1594
  }
1595

1596
  return finalCode;
1597
}
1598
*/
1599

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

1609
  STMT_DLOG_E("start to exec");
1610

1611
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1612

1613
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1614
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1615

1616
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1617

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

1629
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1630

1631
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1632
  pStmt->affectedRows += pStmt->exec.affectedRows;
1633

1634
_return:
1635

1636
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1637

1638
  tFreeSSubmitRsp(pRsp);
1639

1640
  ++pStmt->sql.runTimes;
1641

1642
  STMT_RET(code);
1643
}
1644
*/
1645

1646
int stmtExec(TAOS_STMT* stmt) {
3,697,343✔
1647
  STscStmt*   pStmt = (STscStmt*)stmt;
3,697,343✔
1648
  int32_t     code = 0;
3,697,343✔
1649
  SSubmitRsp* pRsp = NULL;
3,697,343✔
1650

1651
  int64_t startUs = taosGetTimestampUs();
3,699,423✔
1652

1653
  STMT_DLOG_E("start to exec");
3,699,423✔
1654

1655
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
3,699,423✔
1656
    return pStmt->errCode;
×
1657
  }
1658

1659
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
3,697,666✔
1660

1661
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
3,698,550✔
1662
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
9,069✔
1663
  } else {
1664
    if (pStmt->sql.stbInterlaceMode) {
3,687,126✔
1665
      int64_t startTs = taosGetTimestampUs();
337,609✔
1666
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
980,299✔
1667
        taosUsleep(1);
642,738✔
1668
      }
1669
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
337,704✔
1670

1671
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
337,705✔
1672
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
337,659✔
1673
      pStmt->sql.siInfo.pVgroupHash = NULL;
337,610✔
1674
      pStmt->sql.siInfo.pVgroupList = NULL;
337,610✔
1675
    } else {
1676
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
3,350,292✔
1677
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
3,350,759✔
1678

1679
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
3,351,728✔
1680

1681
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
3,350,436✔
1682
    }
1683

1684
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
3,687,998✔
1685
  }
1686

1687
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
3,699,280✔
UNCOV
1688
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1689
    if (code) {
×
1690
      pStmt->exec.pRequest->code = code;
×
1691
    } else {
1692
      tFreeSSubmitRsp(pRsp);
×
1693
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1694
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1695
    }
1696
  }
1697

1698
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
3,699,854✔
1699

1700
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
3,699,230✔
1701
  pStmt->affectedRows += pStmt->exec.affectedRows;
3,698,809✔
1702

1703
_return:
3,699,648✔
1704

1705
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
3,699,648✔
UNCOV
1706
    taosUsleep(1);
×
1707
  }
1708

1709
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
3,699,695✔
1710

1711
  tFreeSSubmitRsp(pRsp);
3,699,026✔
1712

1713
  ++pStmt->sql.runTimes;
3,697,796✔
1714

1715
  int64_t startUs2 = taosGetTimestampUs();
3,697,941✔
1716
  pStmt->stat.execUseUs += startUs2 - startUs;
3,697,941✔
1717

1718
  STMT_RET(code);
3,698,779✔
1719
}
1720

1721
int stmtClose(TAOS_STMT* stmt) {
135,884✔
1722
  STscStmt* pStmt = (STscStmt*)stmt;
135,884✔
1723

1724
  STMT_DLOG_E("start to free stmt");
135,884✔
1725

1726
  if (pStmt->bindThreadInUse) {
135,884✔
1727
    pStmt->queue.stopQueue = true;
9,167✔
1728

1729
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
9,167✔
1730
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
9,167✔
1731
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
9,167✔
1732
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
9,167✔
1733

1734
    (void)taosThreadJoin(pStmt->bindThread, NULL);
9,167✔
1735
    pStmt->bindThreadInUse = false;
9,167✔
1736

1737
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
9,167✔
1738
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
9,167✔
1739
  }
1740

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

1752
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
135,884✔
1753
  taosMemoryFree(stmt);
135,846✔
1754

1755
  return TSDB_CODE_SUCCESS;
135,884✔
1756
}
1757

1758
const char* stmtErrstr(TAOS_STMT* stmt) {
9,629✔
1759
  STscStmt* pStmt = (STscStmt*)stmt;
9,629✔
1760

1761
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
9,629✔
1762
    return (char*)tstrerror(terrno);
564✔
1763
  }
1764

1765
  pStmt->exec.pRequest->code = terrno;
9,065✔
1766

1767
  return taos_errstr(pStmt->exec.pRequest);
9,065✔
1768
}
1769

1770
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
2,967✔
1771

1772
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
80✔
1773

1774
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
66,223,588✔
1775
  STscStmt* pStmt = (STscStmt*)stmt;
66,223,588✔
1776

1777
  STMT_DLOG_E("start is insert");
66,223,588✔
1778

1779
  if (pStmt->sql.type) {
66,251,643✔
1780
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
63,610,793✔
1781
  } else {
1782
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
3,347,401✔
1783
  }
1784

1785
  return TSDB_CODE_SUCCESS;
67,215,303✔
1786
}
1787

1788
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
×
1789
  int32_t   code = 0;
×
1790
  STscStmt* pStmt = (STscStmt*)stmt;
×
1791
  int32_t   preCode = pStmt->errCode;
×
1792

1793
  STMT_DLOG_E("start to get tag fields");
×
1794

1795
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1796
    return pStmt->errCode;
×
1797
  }
1798

1799
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1800
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1801
  }
1802

1803
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1804

1805
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1806
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1807
    pStmt->bInfo.needParse = false;
×
1808
  }
1809

1810
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1811
    taos_free_result(pStmt->exec.pRequest);
×
1812
    pStmt->exec.pRequest = NULL;
×
1813
  }
1814

1815
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1816

1817
  if (pStmt->bInfo.needParse) {
×
1818
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1819
  }
1820

1821
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
×
1822

1823
_return:
×
1824
  // compatible with previous versions
1825
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
×
1826
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
1827
  }
1828

1829
  if (code != TSDB_CODE_SUCCESS) {
×
1830
    taos_free_result(pStmt->exec.pRequest);
×
1831
    pStmt->exec.pRequest = NULL;
×
1832
  }
1833
  pStmt->errCode = preCode;
×
1834

1835
  return code;
×
1836
}
1837

1838
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
×
1839
  int32_t   code = 0;
×
1840
  STscStmt* pStmt = (STscStmt*)stmt;
×
1841
  int32_t   preCode = pStmt->errCode;
×
1842

1843
  STMT_DLOG_E("start to get col fields");
×
1844

1845
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1846
    return pStmt->errCode;
×
1847
  }
1848

1849
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1850
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1851
  }
1852

1853
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1854

1855
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1856
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1857
    pStmt->bInfo.needParse = false;
×
1858
  }
1859

1860
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1861
    taos_free_result(pStmt->exec.pRequest);
×
1862
    pStmt->exec.pRequest = NULL;
×
1863
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1864
  }
1865

1866
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1867

1868
  if (pStmt->bInfo.needParse) {
×
1869
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1870
  }
1871

1872
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
×
1873

1874
_return:
×
1875

1876
  if (code != TSDB_CODE_SUCCESS) {
×
1877
    taos_free_result(pStmt->exec.pRequest);
×
1878
    pStmt->exec.pRequest = NULL;
×
1879
  }
1880

1881
  pStmt->errCode = preCode;
×
1882

1883
  return code;
×
1884
}
1885

1886
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
×
1887
  int       code = 0;
×
1888
  STscStmt* pStmt = (STscStmt*)stmt;
×
1889
  int32_t   preCode = pStmt->errCode;
×
1890

1891
  STMT_DLOG_E("start to get param num");
×
1892

1893
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1894
    return pStmt->errCode;
×
1895
  }
1896

1897
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1898

1899
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1900
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1901
    pStmt->bInfo.needParse = false;
×
1902
  }
1903

1904
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1905
    taos_free_result(pStmt->exec.pRequest);
×
1906
    pStmt->exec.pRequest = NULL;
×
1907
  }
1908

1909
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1910

1911
  if (pStmt->bInfo.needParse) {
×
1912
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1913
  }
1914

1915
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1916
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1917
  } else {
1918
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
×
1919
  }
1920

1921
_return:
×
1922

1923
  if (code != TSDB_CODE_SUCCESS) {
×
1924
    taos_free_result(pStmt->exec.pRequest);
×
1925
    pStmt->exec.pRequest = NULL;
×
1926
  }
1927

1928
  pStmt->errCode = preCode;
×
1929

1930
  return code;
×
1931
}
1932

1933
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
×
1934
  int       code = 0;
×
1935
  STscStmt* pStmt = (STscStmt*)stmt;
×
1936
  int32_t   preCode = pStmt->errCode;
×
1937

1938
  STMT_DLOG_E("start to get param");
×
1939

1940
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1941
    return pStmt->errCode;
×
1942
  }
1943

1944
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1945
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1946
  }
1947

1948
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1949

1950
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1951
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1952
    pStmt->bInfo.needParse = false;
×
1953
  }
1954

1955
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1956
    taos_free_result(pStmt->exec.pRequest);
×
1957
    pStmt->exec.pRequest = NULL;
×
1958
  }
1959

1960
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1961

1962
  if (pStmt->bInfo.needParse) {
×
1963
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1964
  }
1965

1966
  int32_t       nums = 0;
×
1967
  TAOS_FIELD_E* pField = NULL;
×
1968
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
×
1969
  if (idx >= nums) {
×
1970
    tscError("idx %d is too big", idx);
×
1971
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1972
  }
1973

1974
  *type = pField[idx].type;
×
1975
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
×
1976

1977
_return:
×
1978

1979
  if (code != TSDB_CODE_SUCCESS) {
×
1980
    taos_free_result(pStmt->exec.pRequest);
×
1981
    pStmt->exec.pRequest = NULL;
×
1982
  }
1983

1984
  taosMemoryFree(pField);
×
1985
  pStmt->errCode = preCode;
×
1986

1987
  return code;
×
1988
}
1989

1990
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
9,069✔
1991
  STscStmt* pStmt = (STscStmt*)stmt;
9,069✔
1992

1993
  STMT_DLOG_E("start to use result");
9,069✔
1994

1995
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
9,069✔
1996
    tscError("useResult only for query statement");
×
1997
    return NULL;
×
1998
  }
1999

2000
  return pStmt->exec.pRequest;
9,069✔
2001
}
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