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

taosdata / TDengine / #3587

23 Jan 2025 02:42AM UTC coverage: 63.549% (-0.09%) from 63.643%
#3587

push

travis-ci

web-flow
Merge pull request #29637 from taosdata/docs/TS-5944

docs/TS-5944 Correct typos in the descriptions of maximum and minimum values for taosBenchmark

141306 of 285630 branches covered (49.47%)

Branch coverage included in aggregate %.

219951 of 282844 relevant lines covered (77.76%)

19107446.53 hits per line

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

65.41
/source/client/src/clientStmt2.c
1
#include "clientInt.h"
2
#include "clientLog.h"
3
#include "tdef.h"
4

5
#include "clientStmt.h"
6
#include "clientStmt2.h"
7
/*
8
char* gStmtStatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
9
                          "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
10
*/
11
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
12
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
38✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
38✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
38✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
16
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
17
    if (NULL == pTblBuf->pCurBuff) {
×
18
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
19
    }
20
    *pBuf = pTblBuf->pCurBuff;
×
21
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
22
  } else {
23
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
24
    if (NULL == buff) {
×
25
      return terrno;
×
26
    }
27

28
    if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
×
29
      return terrno;
×
30
    }
31

32
    pTblBuf->buffIdx++;
×
33
    pTblBuf->pCurBuff = buff;
×
34
    *pBuf = buff;
×
35
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
36
  }
37

38
  return TSDB_CODE_SUCCESS;
38✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
35✔
42
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
35✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
42✔
44
    (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
16✔
45
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
16✔
46
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
9✔
47
      return false;
9✔
48
    }
49
  }
50
  SStmtQNode* orig = pStmt->queue.head;
26✔
51
  SStmtQNode* node = pStmt->queue.head->next;
26✔
52
  pStmt->queue.head = pStmt->queue.head->next;
26✔
53
  *param = node;
26✔
54

55
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
26✔
56
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
26✔
57

58
  return true;
26✔
59
}
60

61
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
26✔
62
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
26✔
63

64
  pStmt->queue.tail->next = param;
26✔
65
  pStmt->queue.tail = param;
26✔
66
  pStmt->stat.bindDataNum++;
26✔
67
  (void)atomic_add_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
26✔
68
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
26✔
69

70
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
26✔
71
}
26✔
72

73
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
413✔
74
  int32_t code = 0;
413✔
75

76
  if (pStmt->exec.pRequest == NULL) {
413✔
77
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
85✔
78
                        pStmt->reqid);
79
    if (pStmt->reqid != 0) {
85!
80
      pStmt->reqid++;
×
81
    }
82
    if (pStmt->db != NULL) {
85✔
83
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb); 
58!
84
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
58!
85
    }
86
    if (TSDB_CODE_SUCCESS == code) {
85!
87
      pStmt->exec.pRequest->syncQuery = true;
85✔
88
      pStmt->exec.pRequest->isStmtBind = true;
85✔
89
    }
90
  }
91

92
  return code;
413✔
93
}
94

95
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
531✔
96
  int32_t code = 0;
531✔
97

98
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
531!
99
    STMT_LOG_SEQ(newStatus);
531!
100
  }
101

102
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
531!
103
    STMT_DLOG("stmt already failed with err: %s", tstrerror(pStmt->errCode));
×
104
    return pStmt->errCode;
×
105
  }
106

107
  switch (newStatus) {
531!
108
    case STMT_PREPARE:
62✔
109
      pStmt->errCode = 0;
62✔
110
      break;
62✔
111
    case STMT_SETTBNAME:
101✔
112
      if (STMT_STATUS_EQ(INIT)) {
101!
113
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
114
      }
115
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
101!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      break;
101✔
119
    case STMT_SETTAGS:
73✔
120
      if (STMT_STATUS_EQ(INIT)) {
73!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
73✔
124
    case STMT_FETCH_FIELDS:
43✔
125
      if (STMT_STATUS_EQ(INIT)) {
43!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
43✔
129
    case STMT_BIND:
111✔
130
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
111!
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      /*
134
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
135
              code = TSDB_CODE_TSC_STMT_API_ERROR;
136
            }
137
      */
138
      break;
111✔
139
    case STMT_BIND_COL:
×
140
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
141
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
142
      }
143
      break;
×
144
    case STMT_ADD_BATCH:
96✔
145
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
96!
146
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
147
      }
148
      break;
96✔
149
    case STMT_EXECUTE:
45✔
150
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
45✔
151
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
2!
152
            STMT_STATUS_NE(BIND_COL)) {
×
153
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
154
        }
155
      } else {
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
43!
157
          code = TSDB_CODE_TSC_STMT_API_ERROR;
1✔
158
        }
159
      }
160
      break;
45✔
161
    default:
×
162
      code = TSDB_CODE_APP_ERROR;
×
163
      break;
×
164
  }
165

166
  STMT_ERR_RET(code);
531✔
167

168
  pStmt->sql.status = newStatus;
530✔
169

170
  return TSDB_CODE_SUCCESS;
530✔
171
}
172

173
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
31✔
174
  STscStmt2* pStmt = (STscStmt2*)stmt;
31✔
175

176
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
31✔
177

178
  if ('\0' == pStmt->bInfo.tbName[0]) {
31✔
179
    tscError("no table name set");
17!
180
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
17!
181
  }
182

183
  *tbName = pStmt->bInfo.tbName;
14✔
184

185
  return TSDB_CODE_SUCCESS;
14✔
186
}
187

188
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName,
38✔
189
                                  const char* sTableName, bool autoCreateTbl, bool preCtbname) {
190
  STscStmt2* pStmt = (STscStmt2*)stmt;
38✔
191
  char       tbFName[TSDB_TABLE_FNAME_LEN];
192
  int32_t    code = tNameExtractFullName(tbName, tbFName);
38✔
193
  if (code != 0) {
38!
194
    return code;
×
195
  }
196

197
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
38✔
198
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
38✔
199
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
38✔
200

201
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
38✔
202
  pStmt->bInfo.tbSuid = pTableMeta->suid;
38✔
203
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
38✔
204
  pStmt->bInfo.tbType = pTableMeta->tableType;
38✔
205

206
  if (!pStmt->bInfo.tagsCached) {
38!
207
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
38✔
208
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
38!
209
  }
210

211
  pStmt->bInfo.boundTags = tags;
38✔
212
  pStmt->bInfo.tagsCached = false;
38✔
213
  pStmt->bInfo.preCtbname = preCtbname;
38✔
214
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
38✔
215

216
  return TSDB_CODE_SUCCESS;
38✔
217
}
218

219
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
38✔
220
  STscStmt2* pStmt = (STscStmt2*)stmt;
38✔
221

222
  pStmt->sql.pVgHash = pVgHash;
38✔
223
  pStmt->exec.pBlockHash = pBlockHash;
38✔
224

225
  return TSDB_CODE_SUCCESS;
38✔
226
}
227

228
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
38✔
229
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
230
  STscStmt2* pStmt = (STscStmt2*)stmt;
38✔
231

232
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, preCtbname));
38!
233
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
38!
234

235
  pStmt->sql.autoCreateTbl = autoCreateTbl;
38✔
236
  if (pStmt->sql.autoCreateTbl) {
38✔
237
    pStmt->sql.stbInterlaceMode = false;
27✔
238
  }
239

240
  return TSDB_CODE_SUCCESS;
38✔
241
}
242

243
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
244
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
245

246
  *pVgHash = pStmt->sql.pVgHash;
×
247
  pStmt->sql.pVgHash = NULL;
×
248

249
  *pBlockHash = pStmt->exec.pBlockHash;
×
250
  pStmt->exec.pBlockHash = NULL;
×
251

252
  return TSDB_CODE_SUCCESS;
×
253
}
254

255
static int32_t stmtParseSql(STscStmt2* pStmt) {
60✔
256
  pStmt->exec.pCurrBlock = NULL;
60✔
257

258
  SStmtCallback stmtCb = {
60✔
259
      .pStmt = pStmt,
260
      .getTbNameFn = stmtGetTbName,
261
      .setInfoFn = stmtUpdateInfo,
262
      .getExecInfoFn = stmtGetExecInfo,
263
  };
264

265
  STMT_ERR_RET(stmtCreateRequest(pStmt));
60!
266

267
  pStmt->stat.parseSqlNum++;
60✔
268
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
60✔
269
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
41✔
270

271
  pStmt->bInfo.needParse = false;
41✔
272

273
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
41✔
274
    pStmt->sql.type = STMT_TYPE_INSERT;
9✔
275
    pStmt->sql.stbInterlaceMode = false;
9✔
276
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
32✔
277
    pStmt->sql.type = STMT_TYPE_QUERY;
3✔
278
    pStmt->sql.stbInterlaceMode = false;
3✔
279

280
    return TSDB_CODE_SUCCESS;
3✔
281
  }
282

283
  STableDataCxt** pSrc =
284
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
38✔
285
  if (NULL == pSrc || NULL == *pSrc) {
38!
286
    return terrno;
×
287
  }
288

289
  STableDataCxt* pTableCtx = *pSrc;
38✔
290
  if (pStmt->sql.stbInterlaceMode) {
38✔
291
    int16_t lastIdx = -1;
3✔
292

293
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
13✔
294
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
10!
295
        pStmt->sql.stbInterlaceMode = false;
×
296
        break;
×
297
      }
298

299
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
10✔
300
    }
301
  }
302

303
  if (NULL == pStmt->sql.pBindInfo) {
38!
304
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
38!
305
    if (NULL == pStmt->sql.pBindInfo) {
38!
306
      return terrno;
×
307
    }
308
  }
309

310
  return TSDB_CODE_SUCCESS;
38✔
311
}
312

313
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
135✔
314
  pStmt->bInfo.tbUid = 0;
135✔
315
  pStmt->bInfo.tbSuid = 0;
135✔
316
  pStmt->bInfo.tbVgId = -1;
135✔
317
  pStmt->bInfo.tbType = 0;
135✔
318
  pStmt->bInfo.needParse = true;
135✔
319
  pStmt->bInfo.inExecCache = false;
135✔
320

321
  pStmt->bInfo.tbName[0] = 0;
135✔
322
  pStmt->bInfo.tbFName[0] = 0;
135✔
323
  if (!pStmt->bInfo.tagsCached) {
135✔
324
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
113✔
325
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
113!
326
  }
327
  pStmt->bInfo.stbFName[0] = 0;
135✔
328

329
  return TSDB_CODE_SUCCESS;
135✔
330
}
331

332
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
333
  (void)qResetStmtColumns(pTb->aCol, true);
×
334
  taosArrayDestroy(pTb->aCol);
×
335
}
×
336

337
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
7✔
338
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
7✔
339
  if (NULL == pTblBuf->pCurBuff) {
7!
340
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
341
    return;
×
342
  }
343
  pTblBuf->buffIdx = 1;
7✔
344
  pTblBuf->buffOffset = sizeof(*pQueue->head);
7✔
345

346
  (void)taosThreadMutexLock(&pQueue->mutex);
7✔
347
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
7✔
348
  pQueue->qRemainNum = 0;
7✔
349
  pQueue->head->next = NULL;
7✔
350
  (void)taosThreadMutexUnlock(&pQueue->mutex);
7✔
351
}
352

353
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
108✔
354
  if (pStmt->sql.stbInterlaceMode) {
108✔
355
    if (deepClean) {
12✔
356
      taosHashCleanup(pStmt->exec.pBlockHash);
5✔
357
      pStmt->exec.pBlockHash = NULL;
5✔
358

359
      if (NULL != pStmt->exec.pCurrBlock) {
5✔
360
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
3!
361
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
3✔
362
      }
363
    } else {
364
      pStmt->sql.siInfo.pTableColsIdx = 0;
7✔
365
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
7✔
366
    }
367
  } else {
368
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
96✔
369
      // if (!pStmt->options.asyncExecFn) {
370
      taos_free_result(pStmt->exec.pRequest);
94✔
371
      pStmt->exec.pRequest = NULL;
94✔
372
      //}
373
    }
374

375
    size_t keyLen = 0;
96✔
376
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
96✔
377
    while (pIter) {
202✔
378
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
106✔
379
      char*          key = taosHashGetKey(pIter, &keyLen);
106✔
380
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
106✔
381

382
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
106✔
383
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
35✔
384
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
72!
385

386
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
35✔
387
        continue;
35✔
388
      }
389

390
      qDestroyStmtDataBlock(pBlocks);
71✔
391
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
71!
392

393
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
71✔
394
    }
395

396
    if (keepTable) {
96✔
397
      return TSDB_CODE_SUCCESS;
37✔
398
    }
399

400
    taosHashCleanup(pStmt->exec.pBlockHash);
59✔
401
    pStmt->exec.pBlockHash = NULL;
59✔
402

403
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
59✔
404
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
59!
405
  }
406

407
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
71!
408

409
  return TSDB_CODE_SUCCESS;
71✔
410
}
411

412
static void stmtFreeTbBuf(void* buf) {
12✔
413
  void* pBuf = *(void**)buf;
12✔
414
  taosMemoryFree(pBuf);
12!
415
}
12✔
416

417
static void stmtFreeTbCols(void* buf) {
3,000✔
418
  SArray* pCols = *(SArray**)buf;
3,000✔
419
  taosArrayDestroy(pCols);
3,000✔
420
}
3,000✔
421

422
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
64✔
423
  STMT_DLOG_E("start to free SQL info");
64!
424

425
  taosMemoryFreeClear(pStmt->db);
64!
426
  taosMemoryFree(pStmt->sql.pBindInfo);
64!
427
  taosMemoryFree(pStmt->sql.queryRes.fields);
64!
428
  taosMemoryFree(pStmt->sql.queryRes.userFields);
64!
429
  taosMemoryFree(pStmt->sql.sqlStr);
64!
430
  qDestroyQuery(pStmt->sql.pQuery);
64✔
431
  taosArrayDestroy(pStmt->sql.nodeList);
64✔
432
  taosHashCleanup(pStmt->sql.pVgHash);
64✔
433
  pStmt->sql.pVgHash = NULL;
64✔
434

435
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
64✔
436
  while (pIter) {
75✔
437
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
11✔
438

439
    qDestroyStmtDataBlock(pCache->pDataCtx);
11✔
440
    qDestroyBoundColInfo(pCache->boundTags);
11✔
441
    taosMemoryFreeClear(pCache->boundTags);
11!
442

443
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
11✔
444
  }
445
  taosHashCleanup(pStmt->sql.pTableCache);
64✔
446
  pStmt->sql.pTableCache = NULL;
64✔
447

448
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
64!
449
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
64!
450

451
  taos_free_result(pStmt->sql.siInfo.pRequest);
64✔
452
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
64✔
453
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
64✔
454
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
64✔
455
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
64!
456
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
64✔
457
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
64✔
458

459
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
64✔
460
  pStmt->sql.siInfo.tableColsReady = true;
64✔
461

462
  STMT_DLOG_E("end to free SQL info");
64!
463

464
  return TSDB_CODE_SUCCESS;
64✔
465
}
466

467
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
51✔
468
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
51✔
469
    return TSDB_CODE_SUCCESS;
5✔
470
  }
471

472
  SVgroupInfo      vgInfo = {0};
46✔
473
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
46✔
474
                           .requestId = pStmt->exec.pRequest->requestId,
46✔
475
                           .requestObjRefId = pStmt->exec.pRequest->self,
46✔
476
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
46✔
477

478
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
46✔
479
  if (TSDB_CODE_SUCCESS != code) {
46!
480
    return code;
×
481
  }
482

483
  code =
484
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
46✔
485
  if (TSDB_CODE_SUCCESS != code) {
46!
486
    return code;
×
487
  }
488

489
  *vgId = vgInfo.vgId;
46✔
490

491
  return TSDB_CODE_SUCCESS;
46✔
492
}
493

494
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
51✔
495
                                    uint64_t suid, int32_t vgId) {
496
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
51!
497
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
51!
498

499
  STMT_DLOG("tableDataCxt rebuilt, uid:%" PRId64 ", vgId:%d", uid, vgId);
51!
500

501
  return TSDB_CODE_SUCCESS;
51✔
502
}
503

504
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
85✔
505
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
85!
506
    pStmt->bInfo.needParse = false;
×
507
    pStmt->bInfo.inExecCache = false;
×
508
    return TSDB_CODE_SUCCESS;
×
509
  }
510

511
  pStmt->bInfo.needParse = true;
85✔
512
  pStmt->bInfo.inExecCache = false;
85✔
513

514
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
85✔
515
  if (pCxtInExec) {
85✔
516
    pStmt->bInfo.needParse = false;
20✔
517
    pStmt->bInfo.inExecCache = true;
20✔
518

519
    pStmt->exec.pCurrBlock = *pCxtInExec;
20✔
520

521
    if (pStmt->sql.autoCreateTbl) {
20✔
522
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
18!
523
      return TSDB_CODE_SUCCESS;
18✔
524
    }
525
  }
526

527
  if (NULL == pStmt->pCatalog) {
67✔
528
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
14!
529
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
14✔
530
  }
531

532
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
67!
533
    if (pStmt->bInfo.inExecCache) {
14!
534
      pStmt->bInfo.needParse = false;
×
535
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
536
      return TSDB_CODE_SUCCESS;
×
537
    }
538

539
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
14!
540
    return TSDB_CODE_SUCCESS;
14✔
541
  }
542

543
  if (pStmt->sql.autoCreateTbl) {
53✔
544
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
45✔
545
    if (pCache) {
45!
546
      pStmt->bInfo.needParse = false;
45✔
547
      pStmt->bInfo.tbUid = 0;
45✔
548

549
      STableDataCxt* pNewBlock = NULL;
45✔
550
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
45!
551

552
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
45!
553
                      POINTER_BYTES)) {
554
        STMT_ERR_RET(terrno);
×
555
      }
556

557
      pStmt->exec.pCurrBlock = pNewBlock;
45✔
558

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

561
      return TSDB_CODE_SUCCESS;
45✔
562
    }
563

564
    STMT_RET(stmtCleanBindInfo(pStmt));
×
565
  }
566

567
  uint64_t uid, suid;
568
  int32_t  vgId;
569
  int8_t   tableType;
570

571
  STableMeta*      pTableMeta = NULL;
8✔
572
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
8✔
573
                           .requestId = pStmt->exec.pRequest->requestId,
8✔
574
                           .requestObjRefId = pStmt->exec.pRequest->self,
8✔
575
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
8✔
576
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
8✔
577

578
  pStmt->stat.ctgGetTbMetaNum++;
8✔
579

580
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
8!
581
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
582
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
583

584
    STMT_ERR_RET(code);
×
585
  }
586

587
  STMT_ERR_RET(code);
8!
588

589
  uid = pTableMeta->uid;
8✔
590
  suid = pTableMeta->suid;
8✔
591
  tableType = pTableMeta->tableType;
8✔
592
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
8✔
593
  vgId = pTableMeta->vgId;
8✔
594

595
  taosMemoryFree(pTableMeta);
8!
596

597
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
8!
598

599
  if (uid == pStmt->bInfo.tbUid) {
8!
600
    pStmt->bInfo.needParse = false;
×
601

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

604
    return TSDB_CODE_SUCCESS;
×
605
  }
606

607
  if (pStmt->bInfo.inExecCache) {
8✔
608
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
2✔
609
    if (NULL == pCache) {
2!
610
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
611
               pStmt->bInfo.tbFName, uid, cacheUid);
612

613
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
614
    }
615

616
    pStmt->bInfo.needParse = false;
2✔
617

618
    pStmt->bInfo.tbUid = uid;
2✔
619
    pStmt->bInfo.tbSuid = suid;
2✔
620
    pStmt->bInfo.tbType = tableType;
2✔
621
    pStmt->bInfo.boundTags = pCache->boundTags;
2✔
622
    pStmt->bInfo.tagsCached = true;
2✔
623

624
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
2!
625

626
    return TSDB_CODE_SUCCESS;
2✔
627
  }
628

629
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
6✔
630
  if (pCache) {
6!
631
    pStmt->bInfo.needParse = false;
6✔
632

633
    pStmt->bInfo.tbUid = uid;
6✔
634
    pStmt->bInfo.tbSuid = suid;
6✔
635
    pStmt->bInfo.tbType = tableType;
6✔
636
    pStmt->bInfo.boundTags = pCache->boundTags;
6✔
637
    pStmt->bInfo.tagsCached = true;
6✔
638

639
    STableDataCxt* pNewBlock = NULL;
6✔
640
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
6!
641

642
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
6!
643
                    POINTER_BYTES)) {
644
      STMT_ERR_RET(terrno);
×
645
    }
646

647
    pStmt->exec.pCurrBlock = pNewBlock;
6✔
648

649
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
6!
650

651
    return TSDB_CODE_SUCCESS;
6✔
652
  }
653

654
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
655

656
  return TSDB_CODE_SUCCESS;
×
657
}
658

659
static int32_t stmtResetStmt(STscStmt2* pStmt) {
1✔
660
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
1!
661

662
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1✔
663
  if (NULL == pStmt->sql.pTableCache) {
1!
664
    STMT_ERR_RET(terrno);
×
665
  }
666

667
  pStmt->sql.status = STMT_INIT;
1✔
668

669
  return TSDB_CODE_SUCCESS;
1✔
670
}
671

672
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
26✔
673
  SStmtQNode* pParam = (SStmtQNode*)param;
26✔
674

675
  if (pParam->restoreTbCols) {
26✔
676
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
26✔
677
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
19✔
678
      *p = taosArrayInit(20, POINTER_BYTES);
19✔
679
      if (*p == NULL) {
19!
680
        STMT_ERR_RET(terrno);
×
681
      }
682
    }
683

684
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
7✔
685
  } else {
686
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
19!
687
                                        &pStmt->sql.siInfo));
688

689
    // taosMemoryFree(pParam->pTbData);
690

691
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
19✔
692
  }
693
  return TSDB_CODE_SUCCESS;
26✔
694
}
695

696
static void* stmtBindThreadFunc(void* param) {
12✔
697
  setThreadName("stmtBind");
12✔
698

699
  qInfo("stmt bind thread started");
12!
700

701
  STscStmt2* pStmt = (STscStmt2*)param;
12✔
702

703
  while (true) {
35✔
704
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
47✔
705
      break;
12✔
706
    }
707

708
    SStmtQNode* asyncParam = NULL;
35✔
709
    if (!stmtDequeue(pStmt, &asyncParam)) {
35✔
710
      continue;
9✔
711
    }
712

713
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
26!
714
      qError("stmt async output failed");
×
715
    }
716
  }
717

718
  qInfo("stmt bind thread stopped");
12!
719

720
  return NULL;
12✔
721
}
722

723
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
12✔
724
  TdThreadAttr thAttr;
725
  if (taosThreadAttrInit(&thAttr) != 0) {
12!
726
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
727
  }
728
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
12!
729
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
730
  }
731

732
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
12!
733
    terrno = TAOS_SYSTEM_ERROR(errno);
×
734
    STMT_ERR_RET(terrno);
×
735
  }
736

737
  pStmt->bindThreadInUse = true;
12✔
738

739
  (void)taosThreadAttrDestroy(&thAttr);
12✔
740
  return TSDB_CODE_SUCCESS;
12✔
741
}
742

743
static int32_t stmtInitQueue(STscStmt2* pStmt) {
12✔
744
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
12✔
745
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
12✔
746
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
24!
747
  pStmt->queue.tail = pStmt->queue.head;
12✔
748

749
  return TSDB_CODE_SUCCESS;
12✔
750
}
751

752
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
12✔
753
  pTblBuf->buffUnit = sizeof(SStmtQNode);
12✔
754
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
12✔
755
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
12✔
756
  if (NULL == pTblBuf->pBufList) {
12!
757
    return terrno;
×
758
  }
759
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
12!
760
  if (NULL == buff) {
12!
761
    return terrno;
×
762
  }
763

764
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
24!
765
    return terrno;
×
766
  }
767

768
  pTblBuf->pCurBuff = buff;
12✔
769
  pTblBuf->buffIdx = 1;
12✔
770
  pTblBuf->buffOffset = 0;
12✔
771

772
  return TSDB_CODE_SUCCESS;
12✔
773
}
774

775
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
64✔
776
  STscObj*   pObj = (STscObj*)taos;
64✔
777
  STscStmt2* pStmt = NULL;
64✔
778
  int32_t    code = 0;
64✔
779

780
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
64!
781
  if (NULL == pStmt) {
64!
782
    return NULL;
×
783
  }
784

785
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
64✔
786
  if (NULL == pStmt->sql.pTableCache) {
64!
787
    taosMemoryFree(pStmt);
×
788
    return NULL;
×
789
  }
790

791
  pStmt->taos = pObj;
64✔
792
  pStmt->bInfo.needParse = true;
64✔
793
  pStmt->sql.status = STMT_INIT;
64✔
794
  pStmt->errCode = TSDB_CODE_SUCCESS;
64✔
795

796
  if (NULL != pOptions) {
64!
797
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
64✔
798
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
64✔
799
      pStmt->stbInterlaceMode = true;
12✔
800
    }
801

802
    pStmt->reqid = pOptions->reqid;
64✔
803
  }
804

805
  if (pStmt->stbInterlaceMode) {
64✔
806
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
12✔
807
    pStmt->sql.siInfo.acctId = taos->acctId;
12✔
808
    pStmt->sql.siInfo.dbname = taos->db;
12✔
809
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
12✔
810
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
12✔
811
    if (NULL == pStmt->sql.siInfo.pTableHash) {
12!
812
      (void)stmtClose(pStmt);
×
813
      return NULL;
×
814
    }
815
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
12✔
816
    if (NULL == pStmt->sql.siInfo.pTableCols) {
12!
817
      terrno = terrno;
×
818
      (void)stmtClose(pStmt);
×
819
      return NULL;
×
820
    }
821

822
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
12✔
823
    if (TSDB_CODE_SUCCESS == code) {
12!
824
      code = stmtInitQueue(pStmt);
12✔
825
    }
826
    if (TSDB_CODE_SUCCESS == code) {
12!
827
      code = stmtStartBindThread(pStmt);
12✔
828
    }
829
    if (TSDB_CODE_SUCCESS != code) {
12!
830
      terrno = code;
×
831
      (void)stmtClose(pStmt);
×
832
      return NULL;
×
833
    }
834
  }
835

836
  pStmt->sql.siInfo.tableColsReady = true;
64✔
837
  if (pStmt->options.asyncExecFn) {
64✔
838
    if (tsem_init(&pStmt->asyncQuerySem, 0, 1) != 0) {
4!
839
      terrno = TAOS_SYSTEM_ERROR(errno);
×
840
      (void)stmtClose(pStmt);
×
841
      return NULL;
×
842
    }
843
  }
844
  pStmt->semWaited = false;
64✔
845

846
  STMT_LOG_SEQ(STMT_INIT);
64!
847

848
  tscDebug("stmt:%p initialized", pStmt);
64!
849

850
  return pStmt;
64✔
851
}
852

853
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
41✔
854
  STscStmt2* pStmt = (STscStmt2*)stmt;
41✔
855

856
  STMT_DLOG("start to set dbName: %s", dbName);
41!
857

858
  pStmt->db = taosStrdup(dbName);
41!
859
  (void)strdequote(pStmt->db);
41✔
860
  STMT_ERR_RET(stmtCreateRequest(pStmt));
41!
861

862
  // The SQL statement specifies a database name, overriding the previously specified database
863
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
41!
864
  pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
41!
865
  if (pStmt->exec.pRequest->pDb == NULL) {
41!
866
    return terrno;
×
867
  }
868
  return TSDB_CODE_SUCCESS;
41✔
869
}
870

871
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
63✔
872
  STscStmt2* pStmt = (STscStmt2*)stmt;
63✔
873

874
  STMT_DLOG_E("start to prepare");
63!
875

876
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
63✔
877
    return pStmt->errCode;
1✔
878
  }
879

880
  if (pStmt->sql.status >= STMT_PREPARE) {
62✔
881
    STMT_ERR_RET(stmtResetStmt(pStmt));
1!
882
  }
883

884
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
62!
885

886
  if (length <= 0) {
62!
887
    length = strlen(sql);
62✔
888
  }
889

890
  pStmt->sql.sqlStr = taosStrndup(sql, length);
62!
891
  if (!pStmt->sql.sqlStr) {
62!
892
    return terrno;
×
893
  }
894
  pStmt->sql.sqlLen = length;
62✔
895
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
62✔
896

897
  char* dbName = NULL;
62✔
898
  if (qParseDbName(sql, length, &dbName)) {
62✔
899
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
41!
900
    taosMemoryFreeClear(dbName);
41!
901
  }
902

903
  return TSDB_CODE_SUCCESS;
62✔
904
}
905

906
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
3✔
907
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3✔
908
  if (!pSrc) {
3!
909
    return terrno;
×
910
  }
911
  STableDataCxt* pDst = NULL;
3✔
912

913
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3!
914
  pStmt->sql.siInfo.pDataCtx = pDst;
3✔
915

916
  SArray* pTblCols = NULL;
3✔
917
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
3,003✔
918
    pTblCols = taosArrayInit(20, POINTER_BYTES);
3,000✔
919
    if (NULL == pTblCols) {
3,000!
920
      return terrno;
×
921
    }
922

923
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
6,000!
924
      return terrno;
×
925
    }
926
  }
927

928
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
3✔
929

930
  return TSDB_CODE_SUCCESS;
3✔
931
}
932

933
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
256✔
934
  STscStmt2* pStmt = (STscStmt2*)stmt;
256✔
935

936
  STMT_DLOG_E("start is insert");
256!
937

938
  if (pStmt->sql.type) {
256✔
939
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
195✔
940
  } else {
941
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
61✔
942
  }
943

944
  return TSDB_CODE_SUCCESS;
256✔
945
}
946

947
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
101✔
948
  STscStmt2* pStmt = (STscStmt2*)stmt;
101✔
949

950
  int64_t startUs = taosGetTimestampUs();
101✔
951

952
  STMT_DLOG("start to set tbName: %s", tbName);
101!
953

954
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
101!
955
    return pStmt->errCode;
×
956
  }
957

958
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
101!
959

960
  int32_t insert = 0;
101✔
961
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
101!
962
  if (0 == insert) {
101!
963
    tscError("set tb name not available for none insert statement");
×
964
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
965
  }
966

967
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
101✔
968
    STMT_ERR_RET(stmtCreateRequest(pStmt));
85!
969

970
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
85!
971
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
972
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
85!
973

974
    STMT_ERR_RET(stmtGetFromCache(pStmt));
85!
975

976
    if (pStmt->bInfo.needParse) {
85✔
977
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
14✔
978
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
14✔
979

980
      STMT_ERR_RET(stmtParseSql(pStmt));
14!
981
    }
982
  } else {
983
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
16✔
984
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
16✔
985
    pStmt->exec.pRequest->requestId++;
16✔
986
    pStmt->bInfo.needParse = false;
16✔
987
  }
988

989
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
101✔
990
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
3!
991
  }
992

993
  int64_t startUs2 = taosGetTimestampUs();
101✔
994
  pStmt->stat.setTbNameUs += startUs2 - startUs;
101✔
995

996
  return TSDB_CODE_SUCCESS;
101✔
997
}
998

999
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) {
73✔
1000
  STscStmt2* pStmt = (STscStmt2*)stmt;
73✔
1001

1002
  STMT_DLOG_E("start to set tbTags");
73!
1003

1004
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
73!
1005
    return pStmt->errCode;
×
1006
  }
1007

1008
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
73!
1009

1010
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
73!
1011
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1012
    pStmt->bInfo.needParse = false;
×
1013
  }
1014
  STMT_ERR_RET(stmtCreateRequest(pStmt));
73!
1015

1016
  if (pStmt->bInfo.needParse) {
73!
1017
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1018
  }
1019
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
73!
1020
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1021
  }
1022

1023
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
73✔
1024
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1025
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1026
  //   return TSDB_CODE_SUCCESS;
1027
  // }
1028

1029
  STableDataCxt** pDataBlock =
1030
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
73✔
1031
  if (NULL == pDataBlock) {
73!
1032
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1033
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1034
  }
1035

1036
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
73!
1037
    return TSDB_CODE_SUCCESS;
×
1038
  }
1039

1040
  tscDebug("start to bind stmt tag values");
73!
1041
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
73!
1042
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1043
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1044

1045
  return TSDB_CODE_SUCCESS;
73✔
1046
}
1047

1048
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1049
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1050
    return pStmt->errCode;
×
1051
  }
1052

1053
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1054
    tscError("invalid operation to get query column fileds");
×
1055
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1056
  }
1057

1058
  STableDataCxt** pDataBlock = NULL;
×
1059

1060
  if (pStmt->sql.stbInterlaceMode) {
×
1061
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1062
  } else {
1063
    pDataBlock =
1064
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1065
    if (NULL == pDataBlock) {
×
1066
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1067
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1068
    }
1069
  }
1070

1071
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1072

1073
  return TSDB_CODE_SUCCESS;
×
1074
}
1075

1076
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
22✔
1077
  int32_t    code = 0;
22✔
1078
  int32_t    preCode = pStmt->errCode;
22✔
1079

1080
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
22!
1081
    return pStmt->errCode;
×
1082
  }
1083

1084
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
22!
1085
    tscError("invalid operation to get query column fileds");
×
1086
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1087
  }
1088

1089
  STableDataCxt** pDataBlock = NULL;
22✔
1090

1091
  if (pStmt->sql.stbInterlaceMode) {
22!
1092
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1093
  } else {
1094
    pDataBlock =
1095
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
22✔
1096
    if (NULL == pDataBlock) {
22!
1097
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1098
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1099
    }
1100
  }
1101

1102
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.preCtbname, fieldNum, fields));
22!
1103
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE) {
22✔
1104
    pStmt->bInfo.needParse = true;
15✔
1105
    qDestroyStmtDataBlock(*pDataBlock);
15✔
1106
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
15!
1107
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
1108
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1109
    }
1110
  }
1111

1112
_return:
22✔
1113

1114
  pStmt->errCode = preCode;
22✔
1115

1116
  return code;
22✔
1117
}
1118
/*
1119
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1120
  while (true) {
1121
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1122
      pStmt->exec.smInfo.pColIdx = 0;
1123
    }
1124

1125
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1126
      taosUsleep(1);
1127
      continue;
1128
    }
1129

1130
    *idx = pStmt->exec.smInfo.pColIdx;
1131
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1132
  }
1133
}
1134
*/
1135
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
19✔
1136
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
19✔
1137
    pStmt->sql.siInfo.pVgroupHash =
7✔
1138
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
7✔
1139
  }
1140
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
19✔
1141
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
7✔
1142
  }
1143

1144
  if (NULL == pStmt->sql.siInfo.pRequest) {
19✔
1145
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
3!
1146
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1147

1148
    if (pStmt->reqid != 0) {
3!
1149
      pStmt->reqid++;
×
1150
    }
1151
    pStmt->exec.pRequest->syncQuery = true;
3✔
1152

1153
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
3✔
1154
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
3✔
1155
  }
1156

1157
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
19✔
1158
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
6✔
1159
    pStmt->sql.siInfo.tbFromHash = true;
2✔
1160
  }
1161

1162
  if (0 == pStmt->sql.siInfo.firstName[0]) {
19✔
1163
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
3✔
1164
  }
1165

1166
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
19✔
1167
  param->next = NULL;
19✔
1168

1169
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
19✔
1170

1171
  stmtEnqueue(pStmt, param);
19✔
1172

1173
  return TSDB_CODE_SUCCESS;
19✔
1174
}
1175

1176
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1177
  while (true) {
1178
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
19!
1179
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
19✔
1180
      break;
19✔
1181
    } else {
1182
      SArray* pTblCols = NULL;
×
1183
      for (int32_t i = 0; i < 100; i++) {
×
1184
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1185
        if (NULL == pTblCols) {
×
1186
          return terrno;
×
1187
        }
1188

1189
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1190
          return terrno;
×
1191
        }
1192
      }
1193
    }
1194
  }
1195

1196
  return TSDB_CODE_SUCCESS;
19✔
1197
}
1198

1199
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
89✔
1200
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
89✔
1201
    return TSDB_CODE_SUCCESS;
7✔
1202
  }
1203

1204
  uint64_t uid = pStmt->bInfo.tbUid;
82✔
1205
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
82!
1206

1207
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
82✔
1208
    return TSDB_CODE_SUCCESS;
71✔
1209
  }
1210

1211
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1212
  if (!pSrc) {
11!
1213
    return terrno;
×
1214
  }
1215
  STableDataCxt* pDst = NULL;
11✔
1216

1217
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
11!
1218

1219
  SStmtTableCache cache = {
11✔
1220
      .pDataCtx = pDst,
1221
      .boundTags = pStmt->bInfo.boundTags,
11✔
1222
  };
1223

1224
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
11!
1225
    return terrno;
×
1226
  }
1227

1228
  if (pStmt->sql.autoCreateTbl) {
11✔
1229
    pStmt->bInfo.tagsCached = true;
10✔
1230
  } else {
1231
    pStmt->bInfo.boundTags = NULL;
1✔
1232
  }
1233

1234
  return TSDB_CODE_SUCCESS;
11✔
1235
}
1236

1237
static int stmtAddBatch2(TAOS_STMT2* stmt) {
96✔
1238
  STscStmt2* pStmt = (STscStmt2*)stmt;
96✔
1239

1240
  int64_t startUs = taosGetTimestampUs();
96✔
1241

1242
  STMT_DLOG_E("start to add batch");
96!
1243

1244
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
96!
1245
    return pStmt->errCode;
×
1246
  }
1247

1248
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
96!
1249

1250
  if (pStmt->sql.stbInterlaceMode) {
96✔
1251
    int64_t startUs2 = taosGetTimestampUs();
7✔
1252
    pStmt->stat.addBatchUs += startUs2 - startUs;
7✔
1253

1254
    pStmt->sql.siInfo.tableColsReady = false;
7✔
1255

1256
    SStmtQNode* param = NULL;
7✔
1257
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
14!
1258
    param->restoreTbCols = true;
7✔
1259
    param->next = NULL;
7✔
1260

1261
    stmtEnqueue(pStmt, param);
7✔
1262

1263
    return TSDB_CODE_SUCCESS;
7✔
1264
  }
1265

1266
  STMT_ERR_RET(stmtCacheBlock(pStmt));
89!
1267

1268
  return TSDB_CODE_SUCCESS;
89✔
1269
}
1270
/*
1271
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1272
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1273
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1274
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1275

1276
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1277
  pRes->fields = taosMemoryMalloc(size);
1278
  pRes->userFields = taosMemoryMalloc(size);
1279
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1280
    STMT_ERR_RET(terrno);
1281
  }
1282
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1283
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1284

1285
  return TSDB_CODE_SUCCESS;
1286
}
1287

1288
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1289
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1290
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1291

1292
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1293
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1294

1295
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1296
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1297
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1298
      STMT_ERR_RET(terrno);
1299
    }
1300
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1301
  }
1302

1303
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1304
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1305
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1306
      STMT_ERR_RET(terrno);
1307
    }
1308
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1309
  }
1310

1311
  return TSDB_CODE_SUCCESS;
1312
}
1313
*/
1314
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) {
111✔
1315
  STscStmt2* pStmt = (STscStmt2*)stmt;
111✔
1316
  int32_t    code = 0;
111✔
1317

1318
  int64_t startUs = taosGetTimestampUs();
111✔
1319

1320
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
111!
1321

1322
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
111!
1323
    return pStmt->errCode;
×
1324
  }
1325

1326
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
111!
1327

1328
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
111!
1329
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1330
    pStmt->bInfo.needParse = false;
×
1331
  }
1332

1333
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
111!
1334
    taos_free_result(pStmt->exec.pRequest);
1✔
1335
    pStmt->exec.pRequest = NULL;
1✔
1336
  }
1337

1338
  STMT_ERR_RET(stmtCreateRequest(pStmt));
111!
1339

1340
  if (pStmt->bInfo.needParse) {
111✔
1341
    STMT_ERR_RET(stmtParseSql(pStmt));
3!
1342
  }
1343

1344
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
111✔
1345
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
2!
1346

1347
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
2✔
1348
                         .acctId = pStmt->taos->acctId,
2✔
1349
                         .db = pStmt->exec.pRequest->pDb,
2✔
1350
                         .topicQuery = false,
1351
                         .pSql = pStmt->sql.sqlStr,
2✔
1352
                         .sqlLen = pStmt->sql.sqlLen,
2✔
1353
                         .pMsg = pStmt->exec.pRequest->msgBuf,
2✔
1354
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1355
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
2✔
1356
                         .pStmtCb = NULL,
1357
                         .pUser = pStmt->taos->user};
2✔
1358
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2✔
1359
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
2!
1360

1361
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
2!
1362

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

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

1374
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1375
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1376
    // }
1377

1378
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1379

1380
    return TSDB_CODE_SUCCESS;
2✔
1381
  }
1382

1383
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
109!
1384
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1385
  }
1386

1387
  STableDataCxt** pDataBlock = NULL;
109✔
1388

1389
  if (pStmt->exec.pCurrBlock) {
109✔
1390
    pDataBlock = &pStmt->exec.pCurrBlock;
93✔
1391
  } else {
1392
    pDataBlock =
1393
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
16✔
1394
    if (NULL == pDataBlock) {
16!
1395
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1396
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1397
    }
1398
    pStmt->exec.pCurrBlock = *pDataBlock;
16✔
1399
    if (pStmt->sql.stbInterlaceMode) {
16✔
1400
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
3✔
1401
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
3✔
1402
    }
1403
  }
1404

1405
  int64_t startUs2 = taosGetTimestampUs();
109✔
1406
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
109✔
1407

1408
  SStmtQNode* param = NULL;
109✔
1409
  if (pStmt->sql.stbInterlaceMode) {
109✔
1410
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
38!
1411
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
38!
1412
    taosArrayClear(param->tblData.aCol);
19✔
1413

1414
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1415

1416
    param->restoreTbCols = false;
19✔
1417
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
19✔
1418
  }
1419

1420
  int64_t startUs3 = taosGetTimestampUs();
109✔
1421
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
109✔
1422

1423
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
109✔
1424

1425
  if (colIdx < 0) {
109✔
1426
    if (pStmt->sql.stbInterlaceMode) {
103✔
1427
      (*pDataBlock)->pData->flags = 0;
19✔
1428
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
19✔
1429
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
19✔
1430
    } else {
1431
      code =
1432
          qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
84✔
1433
    }
1434

1435
    if (code) {
103✔
1436
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1437
      STMT_ERR_RET(code);
1!
1438
    }
1439
  } else {
1440
    if (pStmt->sql.stbInterlaceMode) {
6!
1441
      tscError("bind single column not allowed in stb insert mode");
×
1442
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1443
    }
1444

1445
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
6!
1446
      tscError("bind column index not in sequence");
×
1447
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1448
    }
1449

1450
    pStmt->bInfo.sBindLastIdx = colIdx;
6✔
1451

1452
    if (0 == colIdx) {
6✔
1453
      pStmt->bInfo.sBindRowNum = bind->num;
3✔
1454
    }
1455

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

1464
  int64_t startUs4 = taosGetTimestampUs();
108✔
1465
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
108✔
1466

1467
  if (pStmt->sql.stbInterlaceMode) {
108✔
1468
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
19!
1469
  } else {
1470
    STMT_ERR_RET(stmtAddBatch2(pStmt));
89!
1471
  }
1472

1473
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
108✔
1474

1475
  return TSDB_CODE_SUCCESS;
108✔
1476
}
1477
/*
1478
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1479
  tscDebug("stmt start to update tbUid, blockNum: %d", pRsp->nBlocks);
1480

1481
  int32_t code = 0;
1482
  int32_t finalCode = 0;
1483
  size_t  keyLen = 0;
1484
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1485
  while (pIter) {
1486
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1487
    char*          key = taosHashGetKey(pIter, &keyLen);
1488

1489
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1490
    if (pMeta->uid) {
1491
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1492
      continue;
1493
    }
1494

1495
    SSubmitBlkRsp* blkRsp = NULL;
1496
    int32_t        i = 0;
1497
    for (; i < pRsp->nBlocks; ++i) {
1498
      blkRsp = pRsp->pBlocks + i;
1499
      if (strlen(blkRsp->tblFName) != keyLen) {
1500
        continue;
1501
      }
1502

1503
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1504
        continue;
1505
      }
1506

1507
      break;
1508
    }
1509

1510
    if (i < pRsp->nBlocks) {
1511
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1512
               blkRsp->uid);
1513

1514
      pMeta->uid = blkRsp->uid;
1515
      pStmt->bInfo.tbUid = blkRsp->uid;
1516
    } else {
1517
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1518
      if (NULL == pStmt->pCatalog) {
1519
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1520
        if (code) {
1521
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1522
          finalCode = code;
1523
          continue;
1524
        }
1525
      }
1526

1527
      code = stmtCreateRequest(pStmt);
1528
      if (code) {
1529
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1530
        finalCode = code;
1531
        continue;
1532
      }
1533

1534
      STableMeta*      pTableMeta = NULL;
1535
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1536
                               .requestId = pStmt->exec.pRequest->requestId,
1537
                               .requestObjRefId = pStmt->exec.pRequest->self,
1538
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1539
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1540

1541
      pStmt->stat.ctgGetTbMetaNum++;
1542

1543
      taos_free_result(pStmt->exec.pRequest);
1544
      pStmt->exec.pRequest = NULL;
1545

1546
      if (code || NULL == pTableMeta) {
1547
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1548
        finalCode = code;
1549
        taosMemoryFree(pTableMeta);
1550
        continue;
1551
      }
1552

1553
      pMeta->uid = pTableMeta->uid;
1554
      pStmt->bInfo.tbUid = pTableMeta->uid;
1555
      taosMemoryFree(pTableMeta);
1556
    }
1557

1558
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1559
  }
1560

1561
  return finalCode;
1562
}
1563
*/
1564
/*
1565
int stmtStaticModeExec(TAOS_STMT* stmt) {
1566
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1567
  int32_t     code = 0;
1568
  SSubmitRsp* pRsp = NULL;
1569
  if (pStmt->sql.staticMode) {
1570
    return TSDB_CODE_TSC_STMT_API_ERROR;
1571
  }
1572

1573
  STMT_DLOG_E("start to exec");
1574

1575
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1576

1577
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1578
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1579

1580
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1581

1582
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1583
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1584
    if (code) {
1585
      pStmt->exec.pRequest->code = code;
1586
    } else {
1587
      tFreeSSubmitRsp(pRsp);
1588
      STMT_ERR_RET(stmtResetStmt(pStmt));
1589
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1590
    }
1591
  }
1592

1593
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1594

1595
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1596
  pStmt->affectedRows += pStmt->exec.affectedRows;
1597

1598
_return:
1599

1600
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1601

1602
  tFreeSSubmitRsp(pRsp);
1603

1604
  ++pStmt->sql.runTimes;
1605

1606
  STMT_RET(code);
1607
}
1608
*/
1609

1610
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
9✔
1611
  const STscObj* pTscObj = pRequest->pTscObj;
9✔
1612

1613
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
9!
1614
  if (*pCxt == NULL) {
9!
1615
    return terrno;
×
1616
  }
1617

1618
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
9✔
1619
                           .requestRid = pRequest->self,
9✔
1620
                           .acctId = pTscObj->acctId,
9✔
1621
                           .db = pRequest->pDb,
9✔
1622
                           .topicQuery = false,
1623
                           .pSql = pRequest->sqlstr,
9✔
1624
                           .sqlLen = pRequest->sqlLen,
9✔
1625
                           .pMsg = pRequest->msgBuf,
9✔
1626
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1627
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
9✔
1628
                           .pStmtCb = NULL,
1629
                           .pUser = pTscObj->user,
9✔
1630
                           .pEffectiveUser = pRequest->effectiveUser,
9✔
1631
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
9✔
1632
                           .enableSysInfo = pTscObj->sysInfo,
9✔
1633
                           .async = true,
1634
                           .svrVer = pTscObj->sVer,
9✔
1635
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
9✔
1636
                           .allocatorId = pRequest->allocatorRefId,
9✔
1637
                           .parseSqlFp = clientParseSql,
1638
                           .parseSqlParam = pWrapper};
1639
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
9✔
1640
  (*pCxt)->biMode = biMode;
9✔
1641
  return TSDB_CODE_SUCCESS;
9✔
1642
}
1643

1644
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
9✔
1645
  STscStmt2*        pStmt = userdata;
9✔
1646
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
9✔
1647

1648
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
9✔
1649
  pStmt->affectedRows += pStmt->exec.affectedRows;
9✔
1650

1651
  fp(pStmt->options.userdata, res, code);
9✔
1652

1653
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
9!
1654
    taosUsleep(1);
×
1655
  }
1656
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
9✔
1657
  ++pStmt->sql.runTimes;
9✔
1658

1659
  if (tsem_post(&pStmt->asyncQuerySem) != 0) {
9!
1660
    tscError("failed to post asyncQuerySem");
×
1661
  }
1662
}
9✔
1663

1664
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
45✔
1665
  STscStmt2* pStmt = (STscStmt2*)stmt;
45✔
1666
  int32_t    code = 0;
45✔
1667
  int64_t    startUs = taosGetTimestampUs();
45✔
1668

1669
  STMT_DLOG_E("start to exec");
45!
1670

1671
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
45!
1672
    return pStmt->errCode;
×
1673
  }
1674

1675
  if (pStmt->sql.stbInterlaceMode) {
45✔
1676
    STMT_ERR_RET(stmtAddBatch2(pStmt));
7!
1677
  }
1678

1679
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
45✔
1680

1681
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
44✔
1682
    if (pStmt->sql.stbInterlaceMode) {
42✔
1683
      int64_t startTs = taosGetTimestampUs();
7✔
1684
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
15✔
1685
        taosUsleep(1);
8✔
1686
      }
1687
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
7✔
1688

1689
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
7!
1690
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
7✔
1691
      pStmt->sql.siInfo.pVgroupHash = NULL;
7✔
1692
      pStmt->sql.siInfo.pVgroupList = NULL;
7✔
1693
    } else {
1694
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
35✔
1695
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
35!
1696

1697
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
35!
1698

1699
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
35!
1700
    }
1701
  }
1702

1703
  SRequestObj*      pRequest = pStmt->exec.pRequest;
44✔
1704
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
44✔
1705

1706
  if (!fp) {
44✔
1707
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
35✔
1708

1709
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
35!
1710
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1711
      if (code) {
×
1712
        pStmt->exec.pRequest->code = code;
×
1713
      } else {
1714
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1715
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1716
      }
1717
    }
1718

1719
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
35!
1720

1721
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
35✔
1722
    if (affected_rows) {
35✔
1723
      *affected_rows = pStmt->exec.affectedRows;
33✔
1724
    }
1725
    pStmt->affectedRows += pStmt->exec.affectedRows;
35✔
1726

1727
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
35!
1728
      taosUsleep(1);
×
1729
    }
1730

1731
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
35!
1732

1733
    ++pStmt->sql.runTimes;
35✔
1734
  } else {
1735
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
9!
1736
    if (pWrapper == NULL) {
9!
1737
      code = terrno;
×
1738
    } else {
1739
      pWrapper->pRequest = pRequest;
9✔
1740
      pRequest->pWrapper = pWrapper;
9✔
1741
    }
1742
    if (TSDB_CODE_SUCCESS == code) {
9!
1743
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
9✔
1744
    }
1745
    pRequest->syncQuery = false;
9✔
1746
    pRequest->body.queryFp = asyncQueryCb;
9✔
1747
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
9✔
1748

1749
    pStmt->semWaited = false;
9✔
1750
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
9✔
1751
  }
1752

1753
_return:
44✔
1754
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
44✔
1755

1756
  STMT_RET(code);
44!
1757
}
1758

1759
int stmtClose2(TAOS_STMT2* stmt) {
63✔
1760
  STscStmt2* pStmt = (STscStmt2*)stmt;
63✔
1761

1762
  STMT_DLOG_E("start to free stmt");
63!
1763

1764
  pStmt->queue.stopQueue = true;
63✔
1765

1766
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
63✔
1767
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
63✔
1768
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
63✔
1769

1770
  if (pStmt->bindThreadInUse) {
63✔
1771
    (void)taosThreadJoin(pStmt->bindThread, NULL);
12✔
1772
    pStmt->bindThreadInUse = false;
12✔
1773
  }
1774

1775
  (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
63✔
1776
  (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
63✔
1777

1778
  if (pStmt->options.asyncExecFn && !pStmt->semWaited) {
63!
1779
    if (tsem_wait(&pStmt->asyncQuerySem) != 0) {
4!
1780
      tscError("failed to wait asyncQuerySem");
×
1781
    }
1782
  }
1783

1784
  STMT_DLOG("stmt %p closed, stbInterlaceMode: %d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
63!
1785
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1786
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1787
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1788
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1789
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1790
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1791
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1792
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1793
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1794

1795
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
63!
1796

1797
  if (pStmt->options.asyncExecFn) {
63✔
1798
    if (tsem_destroy(&pStmt->asyncQuerySem) != 0) {
4!
1799
      tscError("failed to destroy asyncQuerySem");
×
1800
    }
1801
  }
1802
  taosMemoryFree(stmt);
63!
1803

1804
  return TSDB_CODE_SUCCESS;
63✔
1805
}
1806

1807
const char* stmtErrstr2(TAOS_STMT2* stmt) {
3✔
1808
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
1809

1810
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
3!
1811
    return (char*)tstrerror(terrno);
3✔
1812
  }
1813

1814
  pStmt->exec.pRequest->code = terrno;
×
1815

1816
  return taos_errstr(pStmt->exec.pRequest);
×
1817
}
1818
/*
1819
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
1820

1821
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
1822
*/
1823

1824
int stmtParseColFields2(TAOS_STMT2* stmt) {
34✔
1825
  int32_t    code = 0;
34✔
1826
  STscStmt2* pStmt = (STscStmt2*)stmt;
34✔
1827
  int32_t    preCode = pStmt->errCode;
34✔
1828

1829
  STMT_DLOG_E("start to get col fields");
34!
1830

1831
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
34!
1832
    return pStmt->errCode;
×
1833
  }
1834

1835
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
34!
1836
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1837
  }
1838

1839
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
34!
1840

1841
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
34!
1842
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1843
    pStmt->bInfo.needParse = false;
×
1844
  }
1845

1846
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
34!
1847
    taos_free_result(pStmt->exec.pRequest);
×
1848
    pStmt->exec.pRequest = NULL;
×
1849
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1850
  }
1851

1852
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
34!
1853

1854
  if (pStmt->bInfo.needParse) {
34!
1855
    STMT_ERRI_JRET(stmtParseSql(pStmt));
34✔
1856
  }
1857

1858
_return:
22✔
1859

1860
  pStmt->errCode = preCode;
34✔
1861

1862
  return code;
34✔
1863
}
1864

1865
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
34✔
1866
  int32_t code = stmtParseColFields2(stmt);
34✔
1867
  if (code != TSDB_CODE_SUCCESS) {
34✔
1868
    return code;
12✔
1869
  }
1870

1871
  return stmtFetchStbColFields2(stmt, nums, fields);
22✔
1872
}
1873

1874
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
9✔
1875
  int32_t    code = 0;
9✔
1876
  STscStmt2* pStmt = (STscStmt2*)stmt;
9✔
1877
  int32_t    preCode = pStmt->errCode;
9✔
1878

1879
  STMT_DLOG_E("start to get param num");
9!
1880

1881
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9!
1882
    return pStmt->errCode;
×
1883
  }
1884

1885
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
9!
1886

1887
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9!
1888
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1889
    pStmt->bInfo.needParse = false;
×
1890
  }
1891

1892
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
9!
1893
    taos_free_result(pStmt->exec.pRequest);
×
1894
    pStmt->exec.pRequest = NULL;
×
1895
  }
1896

1897
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
9!
1898

1899
  if (pStmt->bInfo.needParse) {
9!
1900
    STMT_ERRI_JRET(stmtParseSql(pStmt));
9✔
1901
  }
1902

1903
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2!
1904
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
2✔
1905
  } else {
1906
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
1907
  }
1908

1909
_return:
×
1910

1911
  pStmt->errCode = preCode;
9✔
1912

1913
  return code;
9✔
1914
}
1915

1916
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
2✔
1917
  STscStmt2* pStmt = (STscStmt2*)stmt;
2✔
1918

1919
  STMT_DLOG_E("start to use result");
2!
1920

1921
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
2!
1922
    tscError("useResult only for query statement");
×
1923
    return NULL;
×
1924
  }
1925

1926
  return pStmt->exec.pRequest;
2✔
1927
}
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