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

taosdata / TDengine / #3610

12 Feb 2025 09:54AM UTC coverage: 54.713% (-8.4%) from 63.066%
#3610

push

travis-ci

web-flow
Merge pull request #29745 from taosdata/fix/TD33664-3.0

fix: --version show information check for 3.0

120957 of 286549 branches covered (42.21%)

Branch coverage included in aggregate %.

190849 of 283342 relevant lines covered (67.36%)

4969786.97 hits per line

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

44.66
/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) {
24✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
24✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
24✔
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;
24✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
24✔
42
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
24✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
32✔
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);
8✔
47
      return false;
8✔
48
    }
49
  }
50
  SStmtQNode* orig = pStmt->queue.head;
16✔
51
  SStmtQNode* node = pStmt->queue.head->next;
16✔
52
  pStmt->queue.head = pStmt->queue.head->next;
16✔
53
  *param = node;
16✔
54

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

58
  return true;
16✔
59
}
60

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

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

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

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

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

92
  return code;
93✔
93
}
94

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

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

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

107
  switch (newStatus) {
159!
108
    case STMT_PREPARE:
9✔
109
      pStmt->errCode = 0;
9✔
110
      break;
9✔
111
    case STMT_SETTBNAME:
33✔
112
      if (STMT_STATUS_EQ(INIT)) {
33!
113
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
114
      }
115
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
33!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      break;
33✔
119
    case STMT_SETTAGS:
24✔
120
      if (STMT_STATUS_EQ(INIT)) {
24!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
24✔
124
    case STMT_FETCH_FIELDS:
×
125
      if (STMT_STATUS_EQ(INIT)) {
×
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
×
129
    case STMT_BIND:
30✔
130
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
30!
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;
30✔
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:
32✔
145
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
32!
146
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
147
      }
148
      break;
32✔
149
    case STMT_EXECUTE:
31✔
150
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
31!
151
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
×
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)) {
31!
157
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
        }
159
      }
160
      break;
31✔
161
    default:
×
162
      code = TSDB_CODE_APP_ERROR;
×
163
      break;
×
164
  }
165

166
  STMT_ERR_RET(code);
159!
167

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

170
  return TSDB_CODE_SUCCESS;
159✔
171
}
172

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

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

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

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

185
  return TSDB_CODE_SUCCESS;
8✔
186
}
187

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

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

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

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

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

216
  return TSDB_CODE_SUCCESS;
8✔
217
}
218

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

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

225
  return TSDB_CODE_SUCCESS;
8✔
226
}
227

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

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

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

240
  return TSDB_CODE_SUCCESS;
8✔
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) {
9✔
256
  pStmt->exec.pCurrBlock = NULL;
9✔
257

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

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

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

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

273
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
8!
274
    pStmt->sql.type = STMT_TYPE_INSERT;
×
275
    pStmt->sql.stbInterlaceMode = false;
×
276
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
8!
277
    pStmt->sql.type = STMT_TYPE_QUERY;
×
278
    pStmt->sql.stbInterlaceMode = false;
×
279

280
    return TSDB_CODE_SUCCESS;
×
281
  }
282

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

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

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

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

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

310
  return TSDB_CODE_SUCCESS;
9✔
311
}
312

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

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

329
  return TSDB_CODE_SUCCESS;
26✔
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) {
8✔
338
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
8✔
339
  if (NULL == pTblBuf->pCurBuff) {
8!
340
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
341
    return;
×
342
  }
343
  pTblBuf->buffIdx = 1;
8✔
344
  pTblBuf->buffOffset = sizeof(*pQueue->head);
8✔
345

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

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

359
      if (NULL != pStmt->exec.pCurrBlock) {
4!
360
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
4!
361
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
4✔
362
      }
363
    } else {
364
      pStmt->sql.siInfo.pTableColsIdx = 0;
8✔
365
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
8✔
366
    }
367
    if (NULL != pStmt->exec.pRequest) {
12!
368
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
12✔
369
    }
370
  } else {
371
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
30!
372
      // if (!pStmt->options.asyncExecFn) {
373
      taos_free_result(pStmt->exec.pRequest);
30✔
374
      pStmt->exec.pRequest = NULL;
30✔
375
      //}
376
    }
377

378
    size_t keyLen = 0;
30✔
379
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
30✔
380
    while (pIter) {
60✔
381
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
30✔
382
      char*          key = taosHashGetKey(pIter, &keyLen);
30✔
383
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
30✔
384

385
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
30!
386
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
25✔
387
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
50!
388

389
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
24✔
390
        continue;
25✔
391
      }
392

393
      qDestroyStmtDataBlock(pBlocks);
5✔
394
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
5!
395

396
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
5✔
397
    }
398

399
    if (keepTable) {
30✔
400
      return TSDB_CODE_SUCCESS;
25✔
401
    }
402

403
    taosHashCleanup(pStmt->exec.pBlockHash);
5✔
404
    pStmt->exec.pBlockHash = NULL;
5✔
405

406
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
5✔
407
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
5!
408
  }
409

410
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
17!
411

412
  return TSDB_CODE_SUCCESS;
17✔
413
}
414

415
static void stmtFreeTbBuf(void* buf) {
8✔
416
  void* pBuf = *(void**)buf;
8✔
417
  taosMemoryFree(pBuf);
8!
418
}
8✔
419

420
static void stmtFreeTbCols(void* buf) {
4,000✔
421
  SArray* pCols = *(SArray**)buf;
4,000✔
422
  taosArrayDestroy(pCols);
4,000✔
423
}
4,000✔
424

425
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
9✔
426
  STMT_DLOG_E("start to free SQL info");
9!
427

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

438
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
9✔
439
  while (pIter) {
14✔
440
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
5✔
441

442
    qDestroyStmtDataBlock(pCache->pDataCtx);
5✔
443
    qDestroyBoundColInfo(pCache->boundTags);
5✔
444
    taosMemoryFreeClear(pCache->boundTags);
5!
445

446
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
5✔
447
  }
448
  taosHashCleanup(pStmt->sql.pTableCache);
9✔
449
  pStmt->sql.pTableCache = NULL;
9✔
450

451
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
9!
452
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
9!
453

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

462
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
9✔
463
  pStmt->sql.siInfo.tableColsReady = true;
9✔
464

465
  STMT_DLOG_E("end to free SQL info");
9!
466

467
  return TSDB_CODE_SUCCESS;
9✔
468
}
469

470
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
×
471
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
×
472
    return TSDB_CODE_SUCCESS;
×
473
  }
474

475
  SVgroupInfo      vgInfo = {0};
×
476
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
477
                           .requestId = pStmt->exec.pRequest->requestId,
×
478
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
479
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
480

481
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
×
482
  if (TSDB_CODE_SUCCESS != code) {
×
483
    return code;
×
484
  }
485

486
  code =
487
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
×
488
  if (TSDB_CODE_SUCCESS != code) {
×
489
    return code;
×
490
  }
491

492
  *vgId = vgInfo.vgId;
×
493

494
  return TSDB_CODE_SUCCESS;
×
495
}
496

497
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
×
498
                                    uint64_t suid, int32_t vgId) {
499
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
500
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
×
501

502
  STMT_DLOG("tableDataCxt rebuilt, uid:%" PRId64 ", vgId:%d", uid, vgId);
×
503

504
  return TSDB_CODE_SUCCESS;
×
505
}
506

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

514
  pStmt->bInfo.needParse = true;
28✔
515
  pStmt->bInfo.inExecCache = false;
28✔
516

517
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
28✔
518
  if (pCxtInExec) {
27✔
519
    pStmt->bInfo.needParse = false;
19✔
520
    pStmt->bInfo.inExecCache = true;
19✔
521

522
    pStmt->exec.pCurrBlock = *pCxtInExec;
19✔
523

524
    if (pStmt->sql.autoCreateTbl) {
19!
525
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
19!
526
      return TSDB_CODE_SUCCESS;
19✔
527
    }
528
  }
529

530
  if (NULL == pStmt->pCatalog) {
8✔
531
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
7!
532
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
8✔
533
  }
534

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

542
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
9!
543
    return TSDB_CODE_SUCCESS;
9✔
544
  }
545

546
  if (pStmt->sql.autoCreateTbl) {
×
547
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
×
548
    if (pCache) {
×
549
      pStmt->bInfo.needParse = false;
×
550
      pStmt->bInfo.tbUid = 0;
×
551

552
      STableDataCxt* pNewBlock = NULL;
×
553
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
×
554

555
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
556
                      POINTER_BYTES)) {
557
        STMT_ERR_RET(terrno);
×
558
      }
559

560
      pStmt->exec.pCurrBlock = pNewBlock;
×
561

562
      tscDebug("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
×
563

564
      return TSDB_CODE_SUCCESS;
×
565
    }
566

567
    STMT_RET(stmtCleanBindInfo(pStmt));
×
568
  }
569

570
  uint64_t uid, suid;
571
  int32_t  vgId;
572
  int8_t   tableType;
573

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

581
  pStmt->stat.ctgGetTbMetaNum++;
×
582

583
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
×
584
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
585
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
586

587
    STMT_ERR_RET(code);
×
588
  }
589

590
  STMT_ERR_RET(code);
×
591

592
  uid = pTableMeta->uid;
×
593
  suid = pTableMeta->suid;
×
594
  tableType = pTableMeta->tableType;
×
595
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
596
  vgId = pTableMeta->vgId;
×
597

598
  taosMemoryFree(pTableMeta);
×
599

600
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
601

602
  if (uid == pStmt->bInfo.tbUid) {
×
603
    pStmt->bInfo.needParse = false;
×
604

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

607
    return TSDB_CODE_SUCCESS;
×
608
  }
609

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

616
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
617
    }
618

619
    pStmt->bInfo.needParse = false;
×
620

621
    pStmt->bInfo.tbUid = uid;
×
622
    pStmt->bInfo.tbSuid = suid;
×
623
    pStmt->bInfo.tbType = tableType;
×
624
    pStmt->bInfo.boundTags = pCache->boundTags;
×
625
    pStmt->bInfo.tagsCached = true;
×
626

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

629
    return TSDB_CODE_SUCCESS;
×
630
  }
631

632
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
633
  if (pCache) {
×
634
    pStmt->bInfo.needParse = false;
×
635

636
    pStmt->bInfo.tbUid = uid;
×
637
    pStmt->bInfo.tbSuid = suid;
×
638
    pStmt->bInfo.tbType = tableType;
×
639
    pStmt->bInfo.boundTags = pCache->boundTags;
×
640
    pStmt->bInfo.tagsCached = true;
×
641

642
    STableDataCxt* pNewBlock = NULL;
×
643
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
644

645
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
646
                    POINTER_BYTES)) {
647
      STMT_ERR_RET(terrno);
×
648
    }
649

650
    pStmt->exec.pCurrBlock = pNewBlock;
×
651

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

654
    return TSDB_CODE_SUCCESS;
×
655
  }
656

657
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
658

659
  return TSDB_CODE_SUCCESS;
×
660
}
661

662
static int32_t stmtResetStmt(STscStmt2* pStmt) {
1✔
663
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
1!
664

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

670
  pStmt->sql.status = STMT_INIT;
1✔
671

672
  return TSDB_CODE_SUCCESS;
1✔
673
}
674

675
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
16✔
676
  SStmtQNode* pParam = (SStmtQNode*)param;
16✔
677

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

687
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
8✔
688
  } else {
689
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
8!
690
                                        &pStmt->sql.siInfo));
691

692
    // taosMemoryFree(pParam->pTbData);
693

694
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
8✔
695
  }
696
  return TSDB_CODE_SUCCESS;
16✔
697
}
698

699
static void* stmtBindThreadFunc(void* param) {
8✔
700
  setThreadName("stmtBind");
8✔
701

702
  qInfo("stmt bind thread started");
8!
703

704
  STscStmt2* pStmt = (STscStmt2*)param;
8✔
705

706
  while (true) {
24✔
707
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
32✔
708
      break;
8✔
709
    }
710

711
    SStmtQNode* asyncParam = NULL;
24✔
712
    if (!stmtDequeue(pStmt, &asyncParam)) {
24✔
713
      continue;
8✔
714
    }
715

716
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
16!
717
      qError("stmt async output failed");
×
718
    }
719
  }
720

721
  qInfo("stmt bind thread stopped");
8!
722

723
  return NULL;
8✔
724
}
725

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

735
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
8!
736
    terrno = TAOS_SYSTEM_ERROR(errno);
×
737
    STMT_ERR_RET(terrno);
×
738
  }
739

740
  pStmt->bindThreadInUse = true;
8✔
741

742
  (void)taosThreadAttrDestroy(&thAttr);
8✔
743
  return TSDB_CODE_SUCCESS;
8✔
744
}
745

746
static int32_t stmtInitQueue(STscStmt2* pStmt) {
8✔
747
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
8✔
748
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
8✔
749
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
16!
750
  pStmt->queue.tail = pStmt->queue.head;
8✔
751

752
  return TSDB_CODE_SUCCESS;
8✔
753
}
754

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

767
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
16!
768
    return terrno;
×
769
  }
770

771
  pTblBuf->pCurBuff = buff;
8✔
772
  pTblBuf->buffIdx = 1;
8✔
773
  pTblBuf->buffOffset = 0;
8✔
774

775
  return TSDB_CODE_SUCCESS;
8✔
776
}
777

778
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
8✔
779
  STscObj*   pObj = (STscObj*)taos;
8✔
780
  STscStmt2* pStmt = NULL;
8✔
781
  int32_t    code = 0;
8✔
782

783
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
8!
784
  if (NULL == pStmt) {
8!
785
    return NULL;
×
786
  }
787

788
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
8✔
789
  if (NULL == pStmt->sql.pTableCache) {
8!
790
    taosMemoryFree(pStmt);
×
791
    return NULL;
×
792
  }
793

794
  pStmt->taos = pObj;
8✔
795
  pStmt->bInfo.needParse = true;
8✔
796
  pStmt->sql.status = STMT_INIT;
8✔
797
  pStmt->errCode = TSDB_CODE_SUCCESS;
8✔
798

799
  if (NULL != pOptions) {
8!
800
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
8✔
801
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
8!
802
      pStmt->stbInterlaceMode = true;
8✔
803
    }
804

805
    pStmt->reqid = pOptions->reqid;
8✔
806
  }
807

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

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

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

849
  STMT_LOG_SEQ(STMT_INIT);
8!
850

851
  tscDebug("stmt:%p initialized", pStmt);
8!
852

853
  return pStmt;
8✔
854
}
855

856
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
857
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
858

859
  STMT_DLOG("start to set dbName: %s", dbName);
×
860

861
  pStmt->db = taosStrdup(dbName);
×
862
  (void)strdequote(pStmt->db);
×
863
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
864

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

874
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
9✔
875
  STscStmt2* pStmt = (STscStmt2*)stmt;
9✔
876

877
  STMT_DLOG_E("start to prepare");
9!
878

879
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9!
880
    return pStmt->errCode;
×
881
  }
882

883
  if (pStmt->sql.status >= STMT_PREPARE) {
9✔
884
    STMT_ERR_RET(stmtResetStmt(pStmt));
1!
885
  }
886

887
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
9!
888

889
  if (length <= 0) {
9!
890
    length = strlen(sql);
×
891
  }
892

893
  pStmt->sql.sqlStr = taosStrndup(sql, length);
9!
894
  if (!pStmt->sql.sqlStr) {
9!
895
    return terrno;
×
896
  }
897
  pStmt->sql.sqlLen = length;
9✔
898
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
9✔
899

900
  char* dbName = NULL;
9✔
901
  if (qParseDbName(sql, length, &dbName)) {
9!
902
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
903
    taosMemoryFreeClear(dbName);
×
904
  }
905

906
  return TSDB_CODE_SUCCESS;
9✔
907
}
908

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

916
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
4!
917
  pStmt->sql.siInfo.pDataCtx = pDst;
4✔
918

919
  SArray* pTblCols = NULL;
4✔
920
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
3,206✔
921
    pTblCols = taosArrayInit(20, POINTER_BYTES);
3,201✔
922
    if (NULL == pTblCols) {
3,566!
923
      return terrno;
×
924
    }
925

926
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
6,768!
927
      return terrno;
×
928
    }
929
  }
930

931
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
5✔
932

933
  return TSDB_CODE_SUCCESS;
5✔
934
}
935

936
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
63✔
937
  STscStmt2* pStmt = (STscStmt2*)stmt;
63✔
938

939
  STMT_DLOG_E("start is insert");
63!
940

941
  if (pStmt->sql.type) {
63✔
942
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
54!
943
  } else {
944
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
9✔
945
  }
946

947
  return TSDB_CODE_SUCCESS;
63✔
948
}
949

950
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
33✔
951
  STscStmt2* pStmt = (STscStmt2*)stmt;
33✔
952

953
  int64_t startUs = taosGetTimestampUs();
33✔
954

955
  STMT_DLOG("start to set tbName: %s", tbName);
33!
956

957
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
33!
958
    return pStmt->errCode;
×
959
  }
960

961
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
33!
962

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

970
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
33✔
971
    STMT_ERR_RET(stmtCreateRequest(pStmt));
29!
972

973
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
29!
974
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
975
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
29!
976

977
    STMT_ERR_RET(stmtGetFromCache(pStmt));
28!
978

979
    if (pStmt->bInfo.needParse) {
28✔
980
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
9✔
981
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
9✔
982

983
      STMT_ERR_RET(stmtParseSql(pStmt));
9!
984
    }
985
  } else {
986
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
4✔
987
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
4✔
988
    pStmt->exec.pRequest->requestId++;
4✔
989
    pStmt->bInfo.needParse = false;
4✔
990
  }
991

992
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
32✔
993
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
4!
994
  }
995

996
  int64_t startUs2 = taosGetTimestampUs();
32✔
997
  pStmt->stat.setTbNameUs += startUs2 - startUs;
32✔
998

999
  return TSDB_CODE_SUCCESS;
32✔
1000
}
1001

1002
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) {
24✔
1003
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
1004

1005
  STMT_DLOG_E("start to set tbTags");
24!
1006

1007
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
24!
1008
    return pStmt->errCode;
×
1009
  }
1010

1011
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
24!
1012

1013
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
24!
1014
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1015
    pStmt->bInfo.needParse = false;
×
1016
  }
1017
  STMT_ERR_RET(stmtCreateRequest(pStmt));
24!
1018

1019
  if (pStmt->bInfo.needParse) {
24!
1020
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1021
  }
1022
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
24!
1023
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1024
  }
1025

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

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

1039
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
24!
1040
    return TSDB_CODE_SUCCESS;
×
1041
  }
1042

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

1048
  return TSDB_CODE_SUCCESS;
22✔
1049
}
1050

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

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

1061
  STableDataCxt** pDataBlock = NULL;
×
1062

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

1074
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1075

1076
  return TSDB_CODE_SUCCESS;
×
1077
}
1078

1079
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
×
1080
  int32_t    code = 0;
×
1081
  int32_t    preCode = pStmt->errCode;
×
1082

1083
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1084
    return pStmt->errCode;
×
1085
  }
1086

1087
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1088
    tscError("invalid operation to get query column fileds");
×
1089
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1090
  }
1091

1092
  STableDataCxt** pDataBlock = NULL;
×
1093

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

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

1115
_return:
×
1116

1117
  pStmt->errCode = preCode;
×
1118

1119
  return code;
×
1120
}
1121
/*
1122
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1123
  while (true) {
1124
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1125
      pStmt->exec.smInfo.pColIdx = 0;
1126
    }
1127

1128
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1129
      taosUsleep(1);
1130
      continue;
1131
    }
1132

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

1147
  if (NULL == pStmt->sql.siInfo.pRequest) {
8✔
1148
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
4!
1149
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1150

1151
    if (pStmt->reqid != 0) {
4!
1152
      pStmt->reqid++;
×
1153
    }
1154
    pStmt->exec.pRequest->syncQuery = true;
4✔
1155

1156
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
4✔
1157
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
4✔
1158
  }
1159

1160
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
8!
1161
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
4!
1162
    pStmt->sql.siInfo.tbFromHash = true;
4✔
1163
  }
1164

1165
  if (0 == pStmt->sql.siInfo.firstName[0]) {
8✔
1166
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
4✔
1167
  }
1168

1169
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
8✔
1170
  param->next = NULL;
8✔
1171

1172
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
8✔
1173

1174
  stmtEnqueue(pStmt, param);
8✔
1175

1176
  return TSDB_CODE_SUCCESS;
8✔
1177
}
1178

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

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

1199
  return TSDB_CODE_SUCCESS;
8✔
1200
}
1201

1202
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
22✔
1203
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
22!
1204
    return TSDB_CODE_SUCCESS;
×
1205
  }
1206

1207
  uint64_t uid = pStmt->bInfo.tbUid;
22✔
1208
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
22!
1209

1210
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
22✔
1211
    return TSDB_CODE_SUCCESS;
20✔
1212
  }
1213

1214
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
4✔
1215
  if (!pSrc) {
2!
1216
    return terrno;
×
1217
  }
1218
  STableDataCxt* pDst = NULL;
2✔
1219

1220
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
2!
1221

1222
  SStmtTableCache cache = {
2✔
1223
      .pDataCtx = pDst,
1224
      .boundTags = pStmt->bInfo.boundTags,
2✔
1225
  };
1226

1227
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
2!
1228
    return terrno;
×
1229
  }
1230

1231
  if (pStmt->sql.autoCreateTbl) {
4!
1232
    pStmt->bInfo.tagsCached = true;
4✔
1233
  } else {
1234
    pStmt->bInfo.boundTags = NULL;
×
1235
  }
1236

1237
  return TSDB_CODE_SUCCESS;
4✔
1238
}
1239

1240
static int stmtAddBatch2(TAOS_STMT2* stmt) {
32✔
1241
  STscStmt2* pStmt = (STscStmt2*)stmt;
32✔
1242

1243
  int64_t startUs = taosGetTimestampUs();
33✔
1244

1245
  STMT_DLOG_E("start to add batch");
33!
1246

1247
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
31!
1248
    return pStmt->errCode;
×
1249
  }
1250

1251
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
31✔
1252

1253
  if (pStmt->sql.stbInterlaceMode) {
30✔
1254
    int64_t startUs2 = taosGetTimestampUs();
8✔
1255
    pStmt->stat.addBatchUs += startUs2 - startUs;
8✔
1256

1257
    pStmt->sql.siInfo.tableColsReady = false;
8✔
1258

1259
    SStmtQNode* param = NULL;
8✔
1260
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
16!
1261
    param->restoreTbCols = true;
8✔
1262
    param->next = NULL;
8✔
1263

1264
    stmtEnqueue(pStmt, param);
8✔
1265

1266
    return TSDB_CODE_SUCCESS;
8✔
1267
  }
1268

1269
  STMT_ERR_RET(stmtCacheBlock(pStmt));
22!
1270

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

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

1288
  return TSDB_CODE_SUCCESS;
1289
}
1290

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

1295
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1296
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1297

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

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

1314
  return TSDB_CODE_SUCCESS;
1315
}
1316
*/
1317
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) {
30✔
1318
  STscStmt2* pStmt = (STscStmt2*)stmt;
30✔
1319
  int32_t    code = 0;
30✔
1320

1321
  int64_t startUs = taosGetTimestampUs();
31✔
1322

1323
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
31!
1324

1325
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
30!
1326
    return pStmt->errCode;
×
1327
  }
1328

1329
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
30!
1330

1331
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
30!
1332
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1333
    pStmt->bInfo.needParse = false;
×
1334
  }
1335

1336
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
30!
1337
    taos_free_result(pStmt->exec.pRequest);
×
1338
    pStmt->exec.pRequest = NULL;
×
1339
  }
1340

1341
  STMT_ERR_RET(stmtCreateRequest(pStmt));
30!
1342

1343
  if (pStmt->bInfo.needParse) {
30!
1344
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1345
  }
1346

1347
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
32!
1348
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
×
1349

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

1364
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
×
1365

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

1373
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
×
1374
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
×
1375
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
×
1376

1377
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1378
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1379
    // }
1380

1381
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1382

1383
    return TSDB_CODE_SUCCESS;
×
1384
  }
1385

1386
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
32!
1387
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1388
  }
1389

1390
  STableDataCxt** pDataBlock = NULL;
30✔
1391

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

1408
  int64_t startUs2 = taosGetTimestampUs();
32✔
1409
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
32✔
1410

1411
  SStmtQNode* param = NULL;
32✔
1412
  if (pStmt->sql.stbInterlaceMode) {
32✔
1413
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
16!
1414
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
16!
1415
    taosArrayClear(param->tblData.aCol);
8✔
1416

1417
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1418

1419
    param->restoreTbCols = false;
8✔
1420
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
8✔
1421
  }
1422

1423
  int64_t startUs3 = taosGetTimestampUs();
32✔
1424
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
32✔
1425

1426
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
32✔
1427

1428
  if (colIdx < 0) {
32!
1429
    if (pStmt->sql.stbInterlaceMode) {
32✔
1430
      (*pDataBlock)->pData->flags = 0;
8✔
1431
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
8✔
1432
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
8✔
1433
    } else {
1434
      code =
1435
          qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
24✔
1436
    }
1437

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

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

1453
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1454

1455
    if (0 == colIdx) {
×
1456
      pStmt->bInfo.sBindRowNum = bind->num;
×
1457
    }
1458

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

1467
  int64_t startUs4 = taosGetTimestampUs();
31✔
1468
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
31✔
1469

1470
  if (pStmt->sql.stbInterlaceMode) {
31✔
1471
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
8!
1472
  } else {
1473
    STMT_ERR_RET(stmtAddBatch2(pStmt));
23!
1474
  }
1475

1476
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
31✔
1477

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

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

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

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

1506
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1507
        continue;
1508
      }
1509

1510
      break;
1511
    }
1512

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

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

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

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

1544
      pStmt->stat.ctgGetTbMetaNum++;
1545

1546
      taos_free_result(pStmt->exec.pRequest);
1547
      pStmt->exec.pRequest = NULL;
1548

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

1556
      pMeta->uid = pTableMeta->uid;
1557
      pStmt->bInfo.tbUid = pTableMeta->uid;
1558
      taosMemoryFree(pTableMeta);
1559
    }
1560

1561
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1562
  }
1563

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

1576
  STMT_DLOG_E("start to exec");
1577

1578
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1579

1580
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1581
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1582

1583
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1584

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

1596
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1597

1598
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1599
  pStmt->affectedRows += pStmt->exec.affectedRows;
1600

1601
_return:
1602

1603
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1604

1605
  tFreeSSubmitRsp(pRsp);
1606

1607
  ++pStmt->sql.runTimes;
1608

1609
  STMT_RET(code);
1610
}
1611
*/
1612

1613
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
1614
  const STscObj* pTscObj = pRequest->pTscObj;
×
1615

1616
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
1617
  if (*pCxt == NULL) {
×
1618
    return terrno;
×
1619
  }
1620

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

1647
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
1648
  STscStmt2*        pStmt = userdata;
×
1649
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1650

1651
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1652
  pStmt->affectedRows += pStmt->exec.affectedRows;
×
1653

1654
  fp(pStmt->options.userdata, res, code);
×
1655

1656
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1657
    taosUsleep(1);
×
1658
  }
1659
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
1660
  ++pStmt->sql.runTimes;
×
1661

1662
  if (tsem_post(&pStmt->asyncQuerySem) != 0) {
×
1663
    tscError("failed to post asyncQuerySem");
×
1664
  }
1665
}
×
1666

1667
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
31✔
1668
  STscStmt2* pStmt = (STscStmt2*)stmt;
31✔
1669
  int32_t    code = 0;
31✔
1670
  int64_t    startUs = taosGetTimestampUs();
31✔
1671

1672
  STMT_DLOG_E("start to exec");
31!
1673

1674
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
31!
1675
    return pStmt->errCode;
×
1676
  }
1677

1678
  if (pStmt->sql.stbInterlaceMode) {
31✔
1679
    STMT_ERR_RET(stmtAddBatch2(pStmt));
8!
1680
  }
1681

1682
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
31!
1683

1684
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
31!
1685
    if (pStmt->sql.stbInterlaceMode) {
31✔
1686
      int64_t startTs = taosGetTimestampUs();
8✔
1687
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
31✔
1688
        taosUsleep(1);
23✔
1689
      }
1690
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
8✔
1691

1692
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
8!
1693
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
8✔
1694
      pStmt->sql.siInfo.pVgroupHash = NULL;
8✔
1695
      pStmt->sql.siInfo.pVgroupList = NULL;
8✔
1696
    } else {
1697
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
23✔
1698
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
25!
1699

1700
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
25!
1701

1702
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
22!
1703
    }
1704
  }
1705

1706
  SRequestObj*      pRequest = pStmt->exec.pRequest;
33✔
1707
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
33✔
1708

1709
  if (!fp) {
33!
1710
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
33✔
1711

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

1722
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
33!
1723

1724
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
33✔
1725
    if (affected_rows) {
33!
1726
      *affected_rows = pStmt->exec.affectedRows;
33✔
1727
    }
1728
    pStmt->affectedRows += pStmt->exec.affectedRows;
33✔
1729

1730
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
33!
1731
      taosUsleep(1);
×
1732
    }
1733

1734
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
33!
1735

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

1752
    pStmt->semWaited = false;
×
1753
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
1754
  }
1755

1756
_return:
33✔
1757
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
33✔
1758

1759
  STMT_RET(code);
33!
1760
}
1761

1762
int stmtClose2(TAOS_STMT2* stmt) {
8✔
1763
  STscStmt2* pStmt = (STscStmt2*)stmt;
8✔
1764

1765
  STMT_DLOG_E("start to free stmt");
8!
1766

1767
  pStmt->queue.stopQueue = true;
8✔
1768

1769
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
8✔
1770
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
8✔
1771
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
8✔
1772

1773
  if (pStmt->bindThreadInUse) {
8!
1774
    (void)taosThreadJoin(pStmt->bindThread, NULL);
8✔
1775
    pStmt->bindThreadInUse = false;
8✔
1776
  }
1777

1778
  (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
8✔
1779
  (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
8✔
1780

1781
  if (pStmt->options.asyncExecFn && !pStmt->semWaited) {
8!
1782
    if (tsem_wait(&pStmt->asyncQuerySem) != 0) {
×
1783
      tscError("failed to wait asyncQuerySem");
×
1784
    }
1785
  }
1786

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

1798
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
8!
1799

1800
  if (pStmt->options.asyncExecFn) {
8!
1801
    if (tsem_destroy(&pStmt->asyncQuerySem) != 0) {
×
1802
      tscError("failed to destroy asyncQuerySem");
×
1803
    }
1804
  }
1805
  taosMemoryFree(stmt);
8!
1806

1807
  return TSDB_CODE_SUCCESS;
8✔
1808
}
1809

1810
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
1811
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1812

1813
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
1814
    return (char*)tstrerror(terrno);
×
1815
  }
1816

1817
  pStmt->exec.pRequest->code = terrno;
×
1818

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

1824
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
1825
*/
1826

1827
int stmtParseColFields2(TAOS_STMT2* stmt) {
×
1828
  int32_t    code = 0;
×
1829
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1830
  int32_t    preCode = pStmt->errCode;
×
1831

1832
  STMT_DLOG_E("start to get col fields");
×
1833

1834
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1835
    return pStmt->errCode;
×
1836
  }
1837

1838
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1839
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1840
  }
1841

1842
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1843

1844
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1845
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1846
    pStmt->bInfo.needParse = false;
×
1847
  }
1848

1849
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1850
    taos_free_result(pStmt->exec.pRequest);
×
1851
    pStmt->exec.pRequest = NULL;
×
1852
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1853
  }
1854

1855
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1856

1857
  if (pStmt->bInfo.needParse) {
×
1858
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1859
  }
1860

1861
_return:
×
1862

1863
  pStmt->errCode = preCode;
×
1864

1865
  return code;
×
1866
}
1867

1868
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
×
1869
  int32_t code = stmtParseColFields2(stmt);
×
1870
  if (code != TSDB_CODE_SUCCESS) {
×
1871
    return code;
×
1872
  }
1873

1874
  return stmtFetchStbColFields2(stmt, nums, fields);
×
1875
}
1876

1877
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
1878
  int32_t    code = 0;
×
1879
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1880
  int32_t    preCode = pStmt->errCode;
×
1881

1882
  STMT_DLOG_E("start to get param num");
×
1883

1884
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1885
    return pStmt->errCode;
×
1886
  }
1887

1888
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1889

1890
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1891
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1892
    pStmt->bInfo.needParse = false;
×
1893
  }
1894

1895
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1896
    taos_free_result(pStmt->exec.pRequest);
×
1897
    pStmt->exec.pRequest = NULL;
×
1898
  }
1899

1900
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1901

1902
  if (pStmt->bInfo.needParse) {
×
1903
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1904
  }
1905

1906
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1907
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1908
  } else {
1909
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
1910
  }
1911

1912
_return:
×
1913

1914
  pStmt->errCode = preCode;
×
1915

1916
  return code;
×
1917
}
1918

1919
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
×
1920
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1921

1922
  STMT_DLOG_E("start to use result");
×
1923

1924
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
1925
    tscError("useResult only for query statement");
×
1926
    return NULL;
×
1927
  }
1928

1929
  return pStmt->exec.pRequest;
×
1930
}
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