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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

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

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
126✔
42
  int i = 0;
126✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
951✔
44
    if (i < 10) {
826✔
45
      taosUsleep(1);
751✔
46
      i++;
750✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
75✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
75!
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
75✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
75✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
126✔
56
    return false;
24✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
102✔
59
  SStmtQNode* node = pStmt->queue.head->next;
102✔
60
  pStmt->queue.head = pStmt->queue.head->next;
102✔
61
  *param = node;
102✔
62

63
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
102✔
64

65
  return true;
101✔
66
}
67

68
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
102✔
69
  pStmt->queue.tail->next = param;
102✔
70
  pStmt->queue.tail = param;
102✔
71

72
  pStmt->stat.bindDataNum++;
102✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
102✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
102✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
102✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
102✔
78
}
102✔
79

80
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
166✔
81
  int32_t code = 0;
166✔
82

83
  if (pStmt->exec.pRequest == NULL) {
166✔
84
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
45✔
85
                        pStmt->reqid);
86
    if (pStmt->reqid != 0) {
45!
87
      pStmt->reqid++;
×
88
    }
89
    if (pStmt->db != NULL) {
45!
90
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb); 
×
91
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
×
92
    }
93
    if (TSDB_CODE_SUCCESS == code) {
45!
94
      pStmt->exec.pRequest->syncQuery = true;
45✔
95
      pStmt->exec.pRequest->isStmtBind = true;
45✔
96
    }
97
  }
98

99
  return code;
166✔
100
}
101

102
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
350✔
103
  int32_t code = 0;
350✔
104

105
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
350!
106
    STMT_LOG_SEQ(newStatus);
350!
107
  }
108

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

114
  switch (newStatus) {
348!
115
    case STMT_PREPARE:
25✔
116
      pStmt->errCode = 0;
25✔
117
      break;
25✔
118
    case STMT_SETTBNAME:
76✔
119
      if (STMT_STATUS_EQ(INIT)) {
76!
120
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
121
      }
122
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
76!
123
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
124
      }
125
      break;
76✔
126
    case STMT_SETTAGS:
24✔
127
      if (STMT_STATUS_EQ(INIT)) {
24!
128
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
129
      }
130
      break;
24✔
131
    case STMT_FETCH_FIELDS:
×
132
      if (STMT_STATUS_EQ(INIT)) {
×
133
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
134
      }
135
      break;
×
136
    case STMT_BIND:
73✔
137
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
73!
138
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
139
      }
140
      /*
141
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
142
              code = TSDB_CODE_TSC_STMT_API_ERROR;
143
            }
144
      */
145
      break;
73✔
146
    case STMT_BIND_COL:
×
147
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
148
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
149
      }
150
      break;
×
151
    case STMT_ADD_BATCH:
74✔
152
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
74!
153
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
154
      }
155
      break;
74✔
156
    case STMT_EXECUTE:
76✔
157
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
76!
158
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
×
159
            STMT_STATUS_NE(BIND_COL)) {
×
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      } else {
163
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
76!
164
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
165
        }
166
      }
167
      break;
76✔
168
    default:
×
169
      code = TSDB_CODE_APP_ERROR;
×
170
      break;
×
171
  }
172

173
  STMT_ERR_RET(code);
348!
174

175
  pStmt->sql.status = newStatus;
348✔
176

177
  return TSDB_CODE_SUCCESS;
348✔
178
}
179

180
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
25✔
181
  STscStmt2* pStmt = (STscStmt2*)stmt;
25✔
182

183
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
25✔
184

185
  if ('\0' == pStmt->bInfo.tbName[0]) {
25!
186
    tscError("no table name set");
×
187
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
×
188
  }
189

190
  *tbName = pStmt->bInfo.tbName;
24✔
191

192
  return TSDB_CODE_SUCCESS;
24✔
193
}
194

195
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName,
22✔
196
                                  const char* sTableName, bool autoCreateTbl, bool preCtbname) {
197
  STscStmt2* pStmt = (STscStmt2*)stmt;
22✔
198
  char       tbFName[TSDB_TABLE_FNAME_LEN];
199
  int32_t    code = tNameExtractFullName(tbName, tbFName);
22✔
200
  if (code != 0) {
25!
201
    return code;
×
202
  }
203

204
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
25✔
205
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
25✔
206
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
25✔
207

208
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
25✔
209
  pStmt->bInfo.tbSuid = pTableMeta->suid;
25✔
210
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
25✔
211
  pStmt->bInfo.tbType = pTableMeta->tableType;
25✔
212

213
  if (!pStmt->bInfo.tagsCached) {
25✔
214
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
23✔
215
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
20!
216
  }
217

218
  pStmt->bInfo.boundTags = tags;
20✔
219
  pStmt->bInfo.tagsCached = false;
20✔
220
  pStmt->bInfo.preCtbname = preCtbname;
20✔
221
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
20✔
222

223
  return TSDB_CODE_SUCCESS;
20✔
224
}
225

226
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
21✔
227
  STscStmt2* pStmt = (STscStmt2*)stmt;
21✔
228

229
  pStmt->sql.pVgHash = pVgHash;
21✔
230
  pStmt->exec.pBlockHash = pBlockHash;
21✔
231

232
  return TSDB_CODE_SUCCESS;
21✔
233
}
234

235
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
24✔
236
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
237
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
238

239
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, preCtbname));
24!
240
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
21!
241

242
  pStmt->sql.autoCreateTbl = autoCreateTbl;
20✔
243
  if (pStmt->sql.autoCreateTbl) {
20✔
244
    pStmt->sql.stbInterlaceMode = false;
2✔
245
  }
246

247
  return TSDB_CODE_SUCCESS;
20✔
248
}
249

250
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
251
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
252

253
  *pVgHash = pStmt->sql.pVgHash;
×
254
  pStmt->sql.pVgHash = NULL;
×
255

256
  *pBlockHash = pStmt->exec.pBlockHash;
×
257
  pStmt->exec.pBlockHash = NULL;
×
258

259
  return TSDB_CODE_SUCCESS;
×
260
}
261

262
static int32_t stmtParseSql(STscStmt2* pStmt) {
24✔
263
  pStmt->exec.pCurrBlock = NULL;
24✔
264

265
  SStmtCallback stmtCb = {
24✔
266
      .pStmt = pStmt,
267
      .getTbNameFn = stmtGetTbName,
268
      .setInfoFn = stmtUpdateInfo,
269
      .getExecInfoFn = stmtGetExecInfo,
270
  };
271

272
  STMT_ERR_RET(stmtCreateRequest(pStmt));
24!
273

274
  pStmt->stat.parseSqlNum++;
23✔
275
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
23!
276
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
21✔
277

278
  pStmt->bInfo.needParse = false;
21✔
279

280
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
21!
281
    pStmt->sql.type = STMT_TYPE_INSERT;
×
282
    pStmt->sql.stbInterlaceMode = false;
×
283
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
21!
284
    pStmt->sql.type = STMT_TYPE_QUERY;
×
285
    pStmt->sql.stbInterlaceMode = false;
×
286

287
    return TSDB_CODE_SUCCESS;
×
288
  }
289

290
  STableDataCxt** pSrc =
291
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
21✔
292
  if (NULL == pSrc || NULL == *pSrc) {
23!
293
    return terrno;
×
294
  }
295

296
  STableDataCxt* pTableCtx = *pSrc;
23✔
297
  if (pStmt->sql.stbInterlaceMode) {
23✔
298
    int16_t lastIdx = -1;
20✔
299

300
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
119✔
301
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
99!
302
        pStmt->sql.stbInterlaceMode = false;
×
303
        break;
×
304
      }
305

306
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
99✔
307
    }
308
  }
309

310
  if (NULL == pStmt->sql.pBindInfo) {
23!
311
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
23!
312
    if (NULL == pStmt->sql.pBindInfo) {
24!
313
      return terrno;
×
314
    }
315
  }
316

317
  return TSDB_CODE_SUCCESS;
24✔
318
}
319

320
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
101✔
321
  pStmt->bInfo.tbUid = 0;
101✔
322
  pStmt->bInfo.tbSuid = 0;
101✔
323
  pStmt->bInfo.tbVgId = -1;
101✔
324
  pStmt->bInfo.tbType = 0;
101✔
325
  pStmt->bInfo.needParse = true;
101✔
326
  pStmt->bInfo.inExecCache = false;
101✔
327

328
  pStmt->bInfo.tbName[0] = 0;
101✔
329
  pStmt->bInfo.tbFName[0] = 0;
101✔
330
  if (!pStmt->bInfo.tagsCached) {
101✔
331
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
91✔
332
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
90!
333
  }
334
  pStmt->bInfo.stbFName[0] = 0;
100✔
335

336
  return TSDB_CODE_SUCCESS;
100✔
337
}
338

339
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
340
  (void)qResetStmtColumns(pTb->aCol, true);
×
341
  taosArrayDestroy(pTb->aCol);
×
342
}
×
343

344
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
51✔
345
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
51✔
346
  if (NULL == pTblBuf->pCurBuff) {
51!
347
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
348
    return;
×
349
  }
350
  pTblBuf->buffIdx = 1;
51✔
351
  pTblBuf->buffOffset = sizeof(*pQueue->head);
51✔
352

353
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
51✔
354
  pQueue->qRemainNum = 0;
51✔
355
  pQueue->head->next = NULL;
51✔
356
}
357

358
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
101✔
359
  if (pStmt->sql.stbInterlaceMode) {
101✔
360
    if (deepClean) {
71✔
361
      taosHashCleanup(pStmt->exec.pBlockHash);
20✔
362
      pStmt->exec.pBlockHash = NULL;
20✔
363

364
      if (NULL != pStmt->exec.pCurrBlock) {
20!
365
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
20!
366
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
20✔
367
      }
368
    } else {
369
      pStmt->sql.siInfo.pTableColsIdx = 0;
51✔
370
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
51✔
371
    }
372
    if (NULL != pStmt->exec.pRequest) {
71!
373
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
71✔
374
    }
375
  } else {
376
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
30!
377
      // if (!pStmt->options.asyncExecFn) {
378
      taos_free_result(pStmt->exec.pRequest);
30✔
379
      pStmt->exec.pRequest = NULL;
30✔
380
      //}
381
    }
382

383
    size_t keyLen = 0;
30✔
384
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
30✔
385
    while (pIter) {
60✔
386
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
30✔
387
      char*          key = taosHashGetKey(pIter, &keyLen);
30✔
388
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
30✔
389

390
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
30!
391
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
25✔
392
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
50!
393

394
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
25✔
395
        continue;
25✔
396
      }
397

398
      qDestroyStmtDataBlock(pBlocks);
5✔
399
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
5!
400

401
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
5✔
402
    }
403

404
    if (keepTable) {
30✔
405
      return TSDB_CODE_SUCCESS;
25✔
406
    }
407

408
    taosHashCleanup(pStmt->exec.pBlockHash);
5✔
409
    pStmt->exec.pBlockHash = NULL;
5✔
410

411
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
5✔
412
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
5!
413
  }
414

415
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
76!
416

417
  return TSDB_CODE_SUCCESS;
75✔
418
}
419

420
static void stmtFreeTbBuf(void* buf) {
24✔
421
  void* pBuf = *(void**)buf;
24✔
422
  taosMemoryFree(pBuf);
24!
423
}
24✔
424

425
static void stmtFreeTbCols(void* buf) {
20,000✔
426
  SArray* pCols = *(SArray**)buf;
20,000✔
427
  taosArrayDestroy(pCols);
20,000✔
428
}
20,000✔
429

430
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
25✔
431
  STMT_DLOG_E("start to free SQL info");
25!
432

433
  taosMemoryFreeClear(pStmt->db);
25!
434
  taosMemoryFree(pStmt->sql.pBindInfo);
25!
435
  taosMemoryFree(pStmt->sql.queryRes.fields);
25!
436
  taosMemoryFree(pStmt->sql.queryRes.userFields);
25!
437
  taosMemoryFree(pStmt->sql.sqlStr);
25!
438
  qDestroyQuery(pStmt->sql.pQuery);
25✔
439
  taosArrayDestroy(pStmt->sql.nodeList);
25✔
440
  taosHashCleanup(pStmt->sql.pVgHash);
25✔
441
  pStmt->sql.pVgHash = NULL;
25✔
442

443
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
25✔
444
  while (pIter) {
30✔
445
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
5✔
446

447
    qDestroyStmtDataBlock(pCache->pDataCtx);
5✔
448
    qDestroyBoundColInfo(pCache->boundTags);
5✔
449
    taosMemoryFreeClear(pCache->boundTags);
5!
450

451
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
5✔
452
  }
453
  taosHashCleanup(pStmt->sql.pTableCache);
25✔
454
  pStmt->sql.pTableCache = NULL;
25✔
455

456
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
25!
457
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
25!
458

459
  taos_free_result(pStmt->sql.siInfo.pRequest);
25✔
460
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
25✔
461
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
25✔
462
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
25✔
463
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
25!
464
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
25✔
465
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
25✔
466

467
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
25✔
468
  pStmt->sql.siInfo.tableColsReady = true;
25✔
469

470
  STMT_DLOG_E("end to free SQL info");
25!
471

472
  return TSDB_CODE_SUCCESS;
25✔
473
}
474

475
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
×
476
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
×
477
    return TSDB_CODE_SUCCESS;
×
478
  }
479

480
  SVgroupInfo      vgInfo = {0};
×
481
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
482
                           .requestId = pStmt->exec.pRequest->requestId,
×
483
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
484
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
485

486
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
×
487
  if (TSDB_CODE_SUCCESS != code) {
×
488
    return code;
×
489
  }
490

491
  code =
492
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
×
493
  if (TSDB_CODE_SUCCESS != code) {
×
494
    return code;
×
495
  }
496

497
  *vgId = vgInfo.vgId;
×
498

499
  return TSDB_CODE_SUCCESS;
×
500
}
501

502
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
×
503
                                    uint64_t suid, int32_t vgId) {
504
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
505
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
×
506

507
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
×
508

509
  return TSDB_CODE_SUCCESS;
×
510
}
511

512
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
45✔
513
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
45!
514
    pStmt->bInfo.needParse = false;
×
515
    pStmt->bInfo.inExecCache = false;
×
516
    return TSDB_CODE_SUCCESS;
×
517
  }
518

519
  pStmt->bInfo.needParse = true;
45✔
520
  pStmt->bInfo.inExecCache = false;
45✔
521

522
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
45✔
523
  if (pCxtInExec) {
43✔
524
    pStmt->bInfo.needParse = false;
20✔
525
    pStmt->bInfo.inExecCache = true;
20✔
526

527
    pStmt->exec.pCurrBlock = *pCxtInExec;
20✔
528

529
    if (pStmt->sql.autoCreateTbl) {
20!
530
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
20!
531
      return TSDB_CODE_SUCCESS;
20✔
532
    }
533
  }
534

535
  if (NULL == pStmt->pCatalog) {
23✔
536
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
22✔
537
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
22✔
538
  }
539

540
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
23!
541
    if (pStmt->bInfo.inExecCache) {
24!
542
      pStmt->bInfo.needParse = false;
×
543
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
544
      return TSDB_CODE_SUCCESS;
×
545
    }
546

547
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
24!
548
    return TSDB_CODE_SUCCESS;
24✔
549
  }
550

551
  if (pStmt->sql.autoCreateTbl) {
×
552
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
×
553
    if (pCache) {
×
554
      pStmt->bInfo.needParse = false;
×
555
      pStmt->bInfo.tbUid = 0;
×
556

557
      STableDataCxt* pNewBlock = NULL;
×
558
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
×
559

560
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
561
                      POINTER_BYTES)) {
562
        STMT_ERR_RET(terrno);
×
563
      }
564

565
      pStmt->exec.pCurrBlock = pNewBlock;
×
566

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

569
      return TSDB_CODE_SUCCESS;
×
570
    }
571

572
    STMT_RET(stmtCleanBindInfo(pStmt));
×
573
  }
574

575
  uint64_t uid, suid;
576
  int32_t  vgId;
577
  int8_t   tableType;
578

579
  STableMeta*      pTableMeta = NULL;
×
580
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
581
                           .requestId = pStmt->exec.pRequest->requestId,
×
582
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
583
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
584
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
×
585

586
  pStmt->stat.ctgGetTbMetaNum++;
×
587

588
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
×
589
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
590
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
591

592
    STMT_ERR_RET(code);
×
593
  }
594

595
  STMT_ERR_RET(code);
×
596

597
  uid = pTableMeta->uid;
×
598
  suid = pTableMeta->suid;
×
599
  tableType = pTableMeta->tableType;
×
600
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
601
  vgId = pTableMeta->vgId;
×
602

603
  taosMemoryFree(pTableMeta);
×
604

605
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
606

607
  if (uid == pStmt->bInfo.tbUid) {
×
608
    pStmt->bInfo.needParse = false;
×
609

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

612
    return TSDB_CODE_SUCCESS;
×
613
  }
614

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

621
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
622
    }
623

624
    pStmt->bInfo.needParse = false;
×
625

626
    pStmt->bInfo.tbUid = uid;
×
627
    pStmt->bInfo.tbSuid = suid;
×
628
    pStmt->bInfo.tbType = tableType;
×
629
    pStmt->bInfo.boundTags = pCache->boundTags;
×
630
    pStmt->bInfo.tagsCached = true;
×
631

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

634
    return TSDB_CODE_SUCCESS;
×
635
  }
636

637
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
638
  if (pCache) {
×
639
    pStmt->bInfo.needParse = false;
×
640

641
    pStmt->bInfo.tbUid = uid;
×
642
    pStmt->bInfo.tbSuid = suid;
×
643
    pStmt->bInfo.tbType = tableType;
×
644
    pStmt->bInfo.boundTags = pCache->boundTags;
×
645
    pStmt->bInfo.tagsCached = true;
×
646

647
    STableDataCxt* pNewBlock = NULL;
×
648
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
649

650
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
651
                    POINTER_BYTES)) {
652
      STMT_ERR_RET(terrno);
×
653
    }
654

655
    pStmt->exec.pCurrBlock = pNewBlock;
×
656

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

659
    return TSDB_CODE_SUCCESS;
×
660
  }
661

662
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
663

664
  return TSDB_CODE_SUCCESS;
×
665
}
666

667
static int32_t stmtResetStmt(STscStmt2* pStmt) {
1✔
668
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
1!
669

670
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1✔
671
  if (NULL == pStmt->sql.pTableCache) {
1!
672
    STMT_ERR_RET(terrno);
×
673
  }
674

675
  pStmt->sql.status = STMT_INIT;
1✔
676

677
  return TSDB_CODE_SUCCESS;
1✔
678
}
679

680
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
101✔
681
  SStmtQNode* pParam = (SStmtQNode*)param;
101✔
682

683
  if (pParam->restoreTbCols) {
101✔
684
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
101✔
685
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
50✔
686
      *p = taosArrayInit(20, POINTER_BYTES);
50✔
687
      if (*p == NULL) {
51!
688
        STMT_ERR_RET(terrno);
×
689
      }
690
    }
691

692
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
51✔
693
  } else {
694
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
51!
695
                                        &pStmt->sql.siInfo));
696

697
    // taosMemoryFree(pParam->pTbData);
698

699
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
51✔
700
  }
701
  return TSDB_CODE_SUCCESS;
102✔
702
}
703

704
static void* stmtBindThreadFunc(void* param) {
24✔
705
  setThreadName("stmtBind");
24✔
706

707
  qInfo("stmt bind thread started");
24!
708

709
  STscStmt2* pStmt = (STscStmt2*)param;
24✔
710

711
  while (true) {
126✔
712
    if (pStmt->queue.stopQueue) {
150✔
713
      break;
24✔
714
    }
715

716
    SStmtQNode* asyncParam = NULL;
126✔
717
    if (!stmtDequeue(pStmt, &asyncParam)) {
126✔
718
      continue;
24✔
719
    }
720

721
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
101!
722
      qError("stmt async output failed");
×
723
    }
724
  }
725

726
  qInfo("stmt bind thread stopped");
24!
727

728
  return NULL;
24✔
729
}
730

731
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
24✔
732
  TdThreadAttr thAttr;
733
  if (taosThreadAttrInit(&thAttr) != 0) {
24!
734
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
735
  }
736
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
24!
737
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
738
  }
739

740
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
24!
741
    terrno = TAOS_SYSTEM_ERROR(errno);
×
742
    STMT_ERR_RET(terrno);
×
743
  }
744

745
  pStmt->bindThreadInUse = true;
24✔
746

747
  (void)taosThreadAttrDestroy(&thAttr);
24✔
748
  return TSDB_CODE_SUCCESS;
24✔
749
}
750

751
static int32_t stmtInitQueue(STscStmt2* pStmt) {
24✔
752
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
24✔
753
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
24✔
754
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
48!
755
  pStmt->queue.tail = pStmt->queue.head;
24✔
756

757
  return TSDB_CODE_SUCCESS;
24✔
758
}
759

760
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
24✔
761
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
24✔
762
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
24✔
763
  pStmt->asyncBindParam.asyncBindNum = 0;
24✔
764

765
  return TSDB_CODE_SUCCESS;
24✔
766
}
767

768
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
24✔
769
  pTblBuf->buffUnit = sizeof(SStmtQNode);
24✔
770
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
24✔
771
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
24✔
772
  if (NULL == pTblBuf->pBufList) {
24!
773
    return terrno;
×
774
  }
775
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
24!
776
  if (NULL == buff) {
24!
777
    return terrno;
×
778
  }
779

780
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
48!
781
    return terrno;
×
782
  }
783

784
  pTblBuf->pCurBuff = buff;
24✔
785
  pTblBuf->buffIdx = 1;
24✔
786
  pTblBuf->buffOffset = 0;
24✔
787

788
  return TSDB_CODE_SUCCESS;
24✔
789
}
790

791
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
24✔
792
  STscObj*   pObj = (STscObj*)taos;
24✔
793
  STscStmt2* pStmt = NULL;
24✔
794
  int32_t    code = 0;
24✔
795

796
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
24!
797
  if (NULL == pStmt) {
24!
798
    return NULL;
×
799
  }
800

801
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
24✔
802
  if (NULL == pStmt->sql.pTableCache) {
24!
803
    taosMemoryFree(pStmt);
×
804
    return NULL;
×
805
  }
806

807
  pStmt->taos = pObj;
24✔
808
  pStmt->bInfo.needParse = true;
24✔
809
  pStmt->sql.status = STMT_INIT;
24✔
810
  pStmt->errCode = TSDB_CODE_SUCCESS;
24✔
811

812
  if (NULL != pOptions) {
24!
813
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
24✔
814
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
24!
815
      pStmt->stbInterlaceMode = true;
24✔
816
    }
817

818
    pStmt->reqid = pOptions->reqid;
24✔
819
  }
820

821
  if (pStmt->stbInterlaceMode) {
24!
822
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
24✔
823
    pStmt->sql.siInfo.acctId = taos->acctId;
24✔
824
    pStmt->sql.siInfo.dbname = taos->db;
24✔
825
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
24✔
826
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
24✔
827
    if (NULL == pStmt->sql.siInfo.pTableHash) {
24!
828
      (void)stmtClose2(pStmt);
×
829
      return NULL;
×
830
    }
831
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
24✔
832
    if (NULL == pStmt->sql.siInfo.pTableCols) {
24!
833
      terrno = terrno;
×
834
      (void)stmtClose2(pStmt);
×
835
      return NULL;
×
836
    }
837

838
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
24✔
839
    if (TSDB_CODE_SUCCESS == code) {
24!
840
      code = stmtInitQueue(pStmt);
24✔
841
    }
842
    if (TSDB_CODE_SUCCESS == code) {
24!
843
      code = stmtStartBindThread(pStmt);
24✔
844
    }
845
    if (TSDB_CODE_SUCCESS != code) {
24!
846
      terrno = code;
×
847
      (void)stmtClose2(pStmt);
×
848
      return NULL;
×
849
    }
850
  }
851

852
  pStmt->sql.siInfo.tableColsReady = true;
24✔
853
  if (pStmt->options.asyncExecFn) {
24!
854
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
×
855
      terrno = TAOS_SYSTEM_ERROR(errno);
×
856
      (void)stmtClose2(pStmt);
×
857
      return NULL;
×
858
    }
859
  }
860
  code = stmtIniAsyncBind(pStmt);
24✔
861
  if (TSDB_CODE_SUCCESS != code) {
24!
862
    terrno = code;
×
863
    (void)stmtClose2(pStmt);
×
864
    return NULL;
×
865
  }
866

867
  pStmt->execSemWaited = false;
24✔
868

869
  STMT_LOG_SEQ(STMT_INIT);
24!
870

871
  tscDebug("stmt:%p initialized", pStmt);
24!
872

873
  return pStmt;
24✔
874
}
875

876
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
877
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
878

879
  STMT_DLOG("start to set dbName:%s", dbName);
×
880

881
  pStmt->db = taosStrdup(dbName);
×
882
  (void)strdequote(pStmt->db);
×
883
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
884

885
  // The SQL statement specifies a database name, overriding the previously specified database
886
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
887
  pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
×
888
  if (pStmt->exec.pRequest->pDb == NULL) {
×
889
    return terrno;
×
890
  }
891
  return TSDB_CODE_SUCCESS;
×
892
}
893

894
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
25✔
895
  STscStmt2* pStmt = (STscStmt2*)stmt;
25✔
896

897
  STMT_DLOG_E("start to prepare");
25!
898

899
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
25!
900
    return pStmt->errCode;
×
901
  }
902

903
  if (pStmt->sql.status >= STMT_PREPARE) {
25✔
904
    STMT_ERR_RET(stmtResetStmt(pStmt));
1!
905
  }
906

907
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
25!
908

909
  if (length <= 0) {
25!
910
    length = strlen(sql);
×
911
  }
912

913
  pStmt->sql.sqlStr = taosStrndup(sql, length);
25!
914
  if (!pStmt->sql.sqlStr) {
25!
915
    return terrno;
×
916
  }
917
  pStmt->sql.sqlLen = length;
25✔
918
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
25✔
919

920
  char* dbName = NULL;
25✔
921
  if (qParseDbName(sql, length, &dbName)) {
25!
922
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
923
    taosMemoryFreeClear(dbName);
×
924
  }
925

926
  return TSDB_CODE_SUCCESS;
25✔
927
}
928

929
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
20✔
930
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20✔
931
  if (!pSrc) {
19!
932
    return terrno;
×
933
  }
934
  STableDataCxt* pDst = NULL;
19✔
935

936
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
19!
937
  pStmt->sql.siInfo.pDataCtx = pDst;
19✔
938

939
  SArray* pTblCols = NULL;
19✔
940
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
17,041!
941
    pTblCols = taosArrayInit(20, POINTER_BYTES);
17,065✔
942
    if (NULL == pTblCols) {
18,805!
943
      return terrno;
×
944
    }
945

946
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
35,827!
947
      return terrno;
×
948
    }
949
  }
950

951
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
×
952

953
  return TSDB_CODE_SUCCESS;
×
954
}
955

956
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
151✔
957
  STscStmt2* pStmt = (STscStmt2*)stmt;
151✔
958

959
  STMT_DLOG_E("start is insert");
151!
960

961
  if (pStmt->sql.type) {
151✔
962
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
126!
963
  } else {
964
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
25✔
965
  }
966

967
  return TSDB_CODE_SUCCESS;
151✔
968
}
969

970
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
76✔
971
  STscStmt2* pStmt = (STscStmt2*)stmt;
76✔
972

973
  int64_t startUs = taosGetTimestampUs();
76✔
974

975
  STMT_DLOG("start to set tbName:%s", tbName);
76!
976

977
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
76!
978
    return pStmt->errCode;
×
979
  }
980

981
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
76!
982

983
  int32_t insert = 0;
76✔
984
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
76!
985
  if (0 == insert) {
76!
986
    tscError("set tb name not available for none insert statement");
×
987
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
988
  }
989

990
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
76✔
991
    STMT_ERR_RET(stmtCreateRequest(pStmt));
45!
992

993
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
45!
994
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
995
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
43!
996

997
    STMT_ERR_RET(stmtGetFromCache(pStmt));
45!
998

999
    if (pStmt->bInfo.needParse) {
43✔
1000
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
21✔
1001
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
21✔
1002

1003
      STMT_ERR_RET(stmtParseSql(pStmt));
21✔
1004
    }
1005
  } else {
1006
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
31✔
1007
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
31✔
1008
    pStmt->exec.pRequest->requestId++;
31✔
1009
    pStmt->bInfo.needParse = false;
31✔
1010
  }
1011

1012
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
74✔
1013
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
20!
1014
  }
1015

1016
  int64_t startUs2 = taosGetTimestampUs();
74✔
1017
  pStmt->stat.setTbNameUs += startUs2 - startUs;
74✔
1018

1019
  return TSDB_CODE_SUCCESS;
74✔
1020
}
1021

1022
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) {
24✔
1023
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
1024

1025
  STMT_DLOG_E("start to set tbTags");
24!
1026

1027
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
24!
1028
    return pStmt->errCode;
×
1029
  }
1030

1031
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
24!
1032

1033
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
24!
1034
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1035
    pStmt->bInfo.needParse = false;
×
1036
  }
1037
  STMT_ERR_RET(stmtCreateRequest(pStmt));
24!
1038

1039
  if (pStmt->bInfo.needParse) {
24!
1040
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1041
  }
1042
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
24!
1043
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1044
  }
1045

1046
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
24✔
1047
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1048
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1049
  //   return TSDB_CODE_SUCCESS;
1050
  // }
1051

1052
  STableDataCxt** pDataBlock =
1053
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
24✔
1054
  if (NULL == pDataBlock) {
24!
1055
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1056
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1057
  }
1058

1059
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
24!
1060
    return TSDB_CODE_SUCCESS;
×
1061
  }
1062

1063
  tscDebug("start to bind stmt tag values");
24!
1064
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
24!
1065
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1066
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1067

1068
  return TSDB_CODE_SUCCESS;
25✔
1069
}
1070

1071
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1072
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1073
    return pStmt->errCode;
×
1074
  }
1075

1076
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1077
    tscError("invalid operation to get query column fileds");
×
1078
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1079
  }
1080

1081
  STableDataCxt** pDataBlock = NULL;
×
1082

1083
  if (pStmt->sql.stbInterlaceMode) {
×
1084
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1085
  } else {
1086
    pDataBlock =
1087
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1088
    if (NULL == pDataBlock) {
×
1089
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1090
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1091
    }
1092
  }
1093

1094
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1095

1096
  return TSDB_CODE_SUCCESS;
×
1097
}
1098

1099
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
×
1100
  int32_t    code = 0;
×
1101
  int32_t    preCode = pStmt->errCode;
×
1102

1103
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1104
    return pStmt->errCode;
×
1105
  }
1106

1107
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1108
    tscError("invalid operation to get query column fileds");
×
1109
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1110
  }
1111

1112
  STableDataCxt** pDataBlock = NULL;
×
1113

1114
  if (pStmt->sql.stbInterlaceMode) {
×
1115
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1116
  } else {
1117
    pDataBlock =
1118
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1119
    if (NULL == pDataBlock) {
×
1120
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1121
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1122
    }
1123
  }
1124

1125
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.preCtbname, fieldNum, fields));
×
1126
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE) {
×
1127
    pStmt->bInfo.needParse = true;
×
1128
    qDestroyStmtDataBlock(*pDataBlock);
×
1129
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
×
1130
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
1131
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1132
    }
1133
  }
1134

1135
_return:
×
1136

1137
  pStmt->errCode = preCode;
×
1138

1139
  return code;
×
1140
}
1141
/*
1142
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1143
  while (true) {
1144
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1145
      pStmt->exec.smInfo.pColIdx = 0;
1146
    }
1147

1148
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1149
      taosUsleep(1);
1150
      continue;
1151
    }
1152

1153
    *idx = pStmt->exec.smInfo.pColIdx;
1154
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1155
  }
1156
}
1157
*/
1158
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
51✔
1159
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
51!
1160
    pStmt->sql.siInfo.pVgroupHash =
51✔
1161
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
51✔
1162
  }
1163
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
51!
1164
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
51✔
1165
  }
1166

1167
  if (NULL == pStmt->sql.siInfo.pRequest) {
51✔
1168
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
20!
1169
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1170

1171
    if (pStmt->reqid != 0) {
20!
1172
      pStmt->reqid++;
×
1173
    }
1174
    pStmt->exec.pRequest->syncQuery = true;
20✔
1175

1176
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
20✔
1177
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
20✔
1178
  }
1179

1180
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
51!
1181
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
31✔
1182
    pStmt->sql.siInfo.tbFromHash = true;
4✔
1183
  }
1184

1185
  if (0 == pStmt->sql.siInfo.firstName[0]) {
51✔
1186
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
20✔
1187
  }
1188

1189
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
51✔
1190
  param->next = NULL;
51✔
1191

1192
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
51✔
1193

1194
  stmtEnqueue(pStmt, param);
51✔
1195

1196
  return TSDB_CODE_SUCCESS;
51✔
1197
}
1198

1199
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1200
  while (true) {
1201
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
50!
1202
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
51✔
1203
      break;
51✔
1204
    } else {
1205
      SArray* pTblCols = NULL;
×
1206
      for (int32_t i = 0; i < 100; i++) {
×
1207
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1208
        if (NULL == pTblCols) {
×
1209
          return terrno;
×
1210
        }
1211

1212
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1213
          return terrno;
×
1214
        }
1215
      }
1216
    }
1217
  }
1218

1219
  return TSDB_CODE_SUCCESS;
51✔
1220
}
1221

1222
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
22✔
1223
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
22!
1224
    return TSDB_CODE_SUCCESS;
×
1225
  }
1226

1227
  uint64_t uid = pStmt->bInfo.tbUid;
22✔
1228
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
22!
1229

1230
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
22✔
1231
    return TSDB_CODE_SUCCESS;
20✔
1232
  }
1233

1234
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
4✔
1235
  if (!pSrc) {
4!
1236
    return terrno;
×
1237
  }
1238
  STableDataCxt* pDst = NULL;
4✔
1239

1240
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
4!
1241

1242
  SStmtTableCache cache = {
4✔
1243
      .pDataCtx = pDst,
1244
      .boundTags = pStmt->bInfo.boundTags,
4✔
1245
  };
1246

1247
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
4!
1248
    return terrno;
×
1249
  }
1250

1251
  if (pStmt->sql.autoCreateTbl) {
4!
1252
    pStmt->bInfo.tagsCached = true;
4✔
1253
  } else {
1254
    pStmt->bInfo.boundTags = NULL;
×
1255
  }
1256

1257
  return TSDB_CODE_SUCCESS;
4✔
1258
}
1259

1260
static int stmtAddBatch2(TAOS_STMT2* stmt) {
75✔
1261
  STscStmt2* pStmt = (STscStmt2*)stmt;
75✔
1262

1263
  int64_t startUs = taosGetTimestampUs();
74✔
1264

1265
  STMT_DLOG_E("start to add batch");
74!
1266

1267
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
76!
1268
    return pStmt->errCode;
×
1269
  }
1270

1271
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
76!
1272

1273
  if (pStmt->sql.stbInterlaceMode) {
74✔
1274
    int64_t startUs2 = taosGetTimestampUs();
51✔
1275
    pStmt->stat.addBatchUs += startUs2 - startUs;
51✔
1276

1277
    pStmt->sql.siInfo.tableColsReady = false;
51✔
1278

1279
    SStmtQNode* param = NULL;
51✔
1280
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
102!
1281
    param->restoreTbCols = true;
51✔
1282
    param->next = NULL;
51✔
1283

1284
    stmtEnqueue(pStmt, param);
51✔
1285

1286
    return TSDB_CODE_SUCCESS;
51✔
1287
  }
1288

1289
  STMT_ERR_RET(stmtCacheBlock(pStmt));
23!
1290

1291
  return TSDB_CODE_SUCCESS;
24✔
1292
}
1293
/*
1294
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1295
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1296
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1297
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1298

1299
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1300
  pRes->fields = taosMemoryMalloc(size);
1301
  pRes->userFields = taosMemoryMalloc(size);
1302
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1303
    STMT_ERR_RET(terrno);
1304
  }
1305
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1306
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1307

1308
  return TSDB_CODE_SUCCESS;
1309
}
1310

1311
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1312
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1313
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1314

1315
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1316
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1317

1318
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1319
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1320
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1321
      STMT_ERR_RET(terrno);
1322
    }
1323
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1324
  }
1325

1326
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1327
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1328
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1329
      STMT_ERR_RET(terrno);
1330
    }
1331
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1332
  }
1333

1334
  return TSDB_CODE_SUCCESS;
1335
}
1336
*/
1337
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) {
75✔
1338
  STscStmt2* pStmt = (STscStmt2*)stmt;
75✔
1339
  int32_t    code = 0;
75✔
1340

1341
  int64_t startUs = taosGetTimestampUs();
75✔
1342

1343
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
75!
1344

1345
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
74!
1346
    return pStmt->errCode;
×
1347
  }
1348

1349
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
74!
1350

1351
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
73!
1352
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1353
    pStmt->bInfo.needParse = false;
×
1354
  }
1355

1356
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
73!
1357
    taos_free_result(pStmt->exec.pRequest);
×
1358
    pStmt->exec.pRequest = NULL;
×
1359
  }
1360

1361
  STMT_ERR_RET(stmtCreateRequest(pStmt));
73!
1362

1363
  if (pStmt->bInfo.needParse) {
74!
1364
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1365
  }
1366

1367
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
73!
1368
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
×
1369

1370
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
×
1371
                         .acctId = pStmt->taos->acctId,
×
1372
                         .db = pStmt->exec.pRequest->pDb,
×
1373
                         .topicQuery = false,
1374
                         .pSql = pStmt->sql.sqlStr,
×
1375
                         .sqlLen = pStmt->sql.sqlLen,
×
1376
                         .pMsg = pStmt->exec.pRequest->msgBuf,
×
1377
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1378
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
×
1379
                         .pStmtCb = NULL,
1380
                         .pUser = pStmt->taos->user};
×
1381
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
×
1382
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
×
1383

1384
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
×
1385

1386
    if (pStmt->sql.pQuery->haveResultSet) {
×
1387
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
×
1388
                                    pStmt->sql.pQuery->numOfResCols));
1389
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
×
1390
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
×
1391
    }
1392

1393
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
×
1394
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
×
1395
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
×
1396

1397
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1398
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1399
    // }
1400

1401
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1402

1403
    return TSDB_CODE_SUCCESS;
×
1404
  }
1405

1406
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
73!
1407
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1408
  }
1409

1410
  STableDataCxt** pDataBlock = NULL;
75✔
1411

1412
  if (pStmt->exec.pCurrBlock) {
75✔
1413
    pDataBlock = &pStmt->exec.pCurrBlock;
51✔
1414
  } else {
1415
    pDataBlock =
1416
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
24✔
1417
    if (NULL == pDataBlock) {
24!
1418
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1419
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1420
    }
1421
    pStmt->exec.pCurrBlock = *pDataBlock;
24✔
1422
    if (pStmt->sql.stbInterlaceMode) {
24✔
1423
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
20✔
1424
      (*pDataBlock)->pData->aCol = NULL;
20✔
1425
    }
1426
    if (colIdx < -1) {
24!
1427
      pStmt->sql.bindRowFormat = true;
×
1428
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
1429
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
1430
    }
1431
  }
1432

1433
  int64_t startUs2 = taosGetTimestampUs();
75✔
1434
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
75✔
1435

1436
  SStmtQNode* param = NULL;
75✔
1437
  if (pStmt->sql.stbInterlaceMode) {
75✔
1438
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
100!
1439
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
101!
1440
    taosArrayClear(param->tblData.aCol);
51✔
1441

1442
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1443

1444
    param->restoreTbCols = false;
51✔
1445
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
51✔
1446
  }
1447

1448
  int64_t startUs3 = taosGetTimestampUs();
75✔
1449
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
75✔
1450

1451
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
75✔
1452

1453
  if (colIdx < 0) {
75✔
1454
    if (pStmt->sql.stbInterlaceMode) {
74✔
1455
      (*pDataBlock)->pData->flags = 0;
51✔
1456
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
51✔
1457
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
51✔
1458
                                    pStmt->taos->optionInfo.charsetCxt);
51✔
1459
    } else {
1460
      if (colIdx == -1) {
23!
1461
        if (pStmt->sql.bindRowFormat) {
23!
1462
          tscError("can't mix bind row format and bind column format");
×
1463
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1464
        }
1465
        code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
23✔
1466
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
23✔
1467
      } else {
1468
        code = qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, bind, pStmt->exec.pRequest->msgBuf,
×
1469
                                  pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
×
1470
                                  pStmt->taos->optionInfo.charsetCxt);
×
1471
      }
1472
    }
1473

1474
    if (code) {
76!
1475
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
×
1476
      STMT_ERR_RET(code);
×
1477
    }
1478
  } else {
1479
    if (pStmt->sql.stbInterlaceMode) {
1!
1480
      tscError("bind single column not allowed in stb insert mode");
×
1481
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1482
    }
1483

1484
    if (pStmt->sql.bindRowFormat) {
1!
1485
      tscError("can't mix bind row format and bind column format");
×
1486
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1487
    }
1488

1489
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
1!
1490
      tscError("bind column index not in sequence");
×
1491
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1492
    }
1493

1494
    pStmt->bInfo.sBindLastIdx = colIdx;
1✔
1495

1496
    if (0 == colIdx) {
1!
1497
      pStmt->bInfo.sBindRowNum = bind->num;
×
1498
    }
1499

1500
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
1✔
1501
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
1✔
1502
    if (code) {
×
1503
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1504
      STMT_ERR_RET(code);
×
1505
    }
1506
  }
1507

1508
  int64_t startUs4 = taosGetTimestampUs();
75✔
1509
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
75✔
1510

1511
  if (pStmt->sql.stbInterlaceMode) {
75✔
1512
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
51!
1513
  } else {
1514
    STMT_ERR_RET(stmtAddBatch2(pStmt));
24!
1515
  }
1516

1517
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
75✔
1518

1519
  return TSDB_CODE_SUCCESS;
75✔
1520
}
1521
/*
1522
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1523
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1524

1525
  int32_t code = 0;
1526
  int32_t finalCode = 0;
1527
  size_t  keyLen = 0;
1528
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1529
  while (pIter) {
1530
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1531
    char*          key = taosHashGetKey(pIter, &keyLen);
1532

1533
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1534
    if (pMeta->uid) {
1535
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1536
      continue;
1537
    }
1538

1539
    SSubmitBlkRsp* blkRsp = NULL;
1540
    int32_t        i = 0;
1541
    for (; i < pRsp->nBlocks; ++i) {
1542
      blkRsp = pRsp->pBlocks + i;
1543
      if (strlen(blkRsp->tblFName) != keyLen) {
1544
        continue;
1545
      }
1546

1547
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1548
        continue;
1549
      }
1550

1551
      break;
1552
    }
1553

1554
    if (i < pRsp->nBlocks) {
1555
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1556
               blkRsp->uid);
1557

1558
      pMeta->uid = blkRsp->uid;
1559
      pStmt->bInfo.tbUid = blkRsp->uid;
1560
    } else {
1561
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1562
      if (NULL == pStmt->pCatalog) {
1563
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1564
        if (code) {
1565
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1566
          finalCode = code;
1567
          continue;
1568
        }
1569
      }
1570

1571
      code = stmtCreateRequest(pStmt);
1572
      if (code) {
1573
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1574
        finalCode = code;
1575
        continue;
1576
      }
1577

1578
      STableMeta*      pTableMeta = NULL;
1579
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1580
                               .requestId = pStmt->exec.pRequest->requestId,
1581
                               .requestObjRefId = pStmt->exec.pRequest->self,
1582
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1583
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1584

1585
      pStmt->stat.ctgGetTbMetaNum++;
1586

1587
      taos_free_result(pStmt->exec.pRequest);
1588
      pStmt->exec.pRequest = NULL;
1589

1590
      if (code || NULL == pTableMeta) {
1591
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1592
        finalCode = code;
1593
        taosMemoryFree(pTableMeta);
1594
        continue;
1595
      }
1596

1597
      pMeta->uid = pTableMeta->uid;
1598
      pStmt->bInfo.tbUid = pTableMeta->uid;
1599
      taosMemoryFree(pTableMeta);
1600
    }
1601

1602
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1603
  }
1604

1605
  return finalCode;
1606
}
1607
*/
1608
/*
1609
int stmtStaticModeExec(TAOS_STMT* stmt) {
1610
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1611
  int32_t     code = 0;
1612
  SSubmitRsp* pRsp = NULL;
1613
  if (pStmt->sql.staticMode) {
1614
    return TSDB_CODE_TSC_STMT_API_ERROR;
1615
  }
1616

1617
  STMT_DLOG_E("start to exec");
1618

1619
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1620

1621
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1622
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1623

1624
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1625

1626
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1627
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1628
    if (code) {
1629
      pStmt->exec.pRequest->code = code;
1630
    } else {
1631
      tFreeSSubmitRsp(pRsp);
1632
      STMT_ERR_RET(stmtResetStmt(pStmt));
1633
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1634
    }
1635
  }
1636

1637
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1638

1639
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1640
  pStmt->affectedRows += pStmt->exec.affectedRows;
1641

1642
_return:
1643

1644
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1645

1646
  tFreeSSubmitRsp(pRsp);
1647

1648
  ++pStmt->sql.runTimes;
1649

1650
  STMT_RET(code);
1651
}
1652
*/
1653

1654
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
1655
  const STscObj* pTscObj = pRequest->pTscObj;
×
1656

1657
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
1658
  if (*pCxt == NULL) {
×
1659
    return terrno;
×
1660
  }
1661

1662
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
1663
                           .requestRid = pRequest->self,
×
1664
                           .acctId = pTscObj->acctId,
×
1665
                           .db = pRequest->pDb,
×
1666
                           .topicQuery = false,
1667
                           .pSql = pRequest->sqlstr,
×
1668
                           .sqlLen = pRequest->sqlLen,
×
1669
                           .pMsg = pRequest->msgBuf,
×
1670
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1671
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
1672
                           .pStmtCb = NULL,
1673
                           .pUser = pTscObj->user,
×
1674
                           .pEffectiveUser = pRequest->effectiveUser,
×
1675
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
1676
                           .enableSysInfo = pTscObj->sysInfo,
×
1677
                           .async = true,
1678
                           .svrVer = pTscObj->sVer,
×
1679
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
1680
                           .allocatorId = pRequest->allocatorRefId,
×
1681
                           .parseSqlFp = clientParseSql,
1682
                           .parseSqlParam = pWrapper};
1683
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
1684
  (*pCxt)->biMode = biMode;
×
1685
  return TSDB_CODE_SUCCESS;
×
1686
}
1687

1688
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
1689
  STscStmt2*        pStmt = userdata;
×
1690
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1691

1692
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1693
  pStmt->affectedRows += pStmt->exec.affectedRows;
×
1694

1695
  fp(pStmt->options.userdata, res, code);
×
1696

1697
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1698
    taosUsleep(1);
×
1699
  }
1700
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
1701
  ++pStmt->sql.runTimes;
×
1702

1703
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
1704
    tscError("failed to post asyncExecSem");
×
1705
  }
1706
}
×
1707

1708
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
73✔
1709
  STscStmt2* pStmt = (STscStmt2*)stmt;
73✔
1710
  int32_t    code = 0;
73✔
1711
  int64_t    startUs = taosGetTimestampUs();
76✔
1712

1713
  STMT_DLOG_E("start to exec");
76!
1714

1715
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
76!
1716
    return pStmt->errCode;
×
1717
  }
1718

1719
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
76!
1720
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
76!
1721
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1722
  }
1723
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
76!
1724

1725
  if (pStmt->sql.stbInterlaceMode) {
76✔
1726
    STMT_ERR_RET(stmtAddBatch2(pStmt));
51!
1727
  }
1728

1729
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
76!
1730

1731
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
76!
1732
    if (pStmt->sql.stbInterlaceMode) {
76✔
1733
      int64_t startTs = taosGetTimestampUs();
51✔
1734
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
179✔
1735
        taosUsleep(1);
128✔
1736
      }
1737
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
51✔
1738

1739
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
51!
1740
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
51✔
1741
      pStmt->sql.siInfo.pVgroupHash = NULL;
51✔
1742
      pStmt->sql.siInfo.pVgroupList = NULL;
51✔
1743
    } else {
1744
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
25✔
1745
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
25!
1746

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

1749
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
23!
1750
    }
1751
  }
1752

1753
  SRequestObj*      pRequest = pStmt->exec.pRequest;
76✔
1754
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
76✔
1755

1756
  if (!fp) {
76!
1757
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
76✔
1758

1759
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
76!
1760
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1761
      if (code) {
×
1762
        pStmt->exec.pRequest->code = code;
×
1763
      } else {
1764
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1765
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1766
      }
1767
    }
1768

1769
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
76!
1770

1771
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
76✔
1772
    if (affected_rows) {
76!
1773
      *affected_rows = pStmt->exec.affectedRows;
76✔
1774
    }
1775
    pStmt->affectedRows += pStmt->exec.affectedRows;
76✔
1776

1777
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
76!
1778
      taosUsleep(1);
×
1779
    }
1780

1781
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
75!
1782

1783
    ++pStmt->sql.runTimes;
75✔
1784
  } else {
1785
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
1786
    if (pWrapper == NULL) {
×
1787
      code = terrno;
×
1788
    } else {
1789
      pWrapper->pRequest = pRequest;
×
1790
      pRequest->pWrapper = pWrapper;
×
1791
    }
1792
    if (TSDB_CODE_SUCCESS == code) {
×
1793
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
1794
    }
1795
    pRequest->syncQuery = false;
×
1796
    pRequest->body.queryFp = asyncQueryCb;
×
1797
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
1798

1799
    pStmt->execSemWaited = false;
×
1800
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
1801
  }
1802

1803
_return:
75✔
1804
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
76✔
1805

1806
  STMT_RET(code);
76!
1807
}
1808

1809
int stmtClose2(TAOS_STMT2* stmt) {
24✔
1810
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
1811

1812
  STMT_DLOG_E("start to free stmt");
24!
1813

1814
  pStmt->queue.stopQueue = true;
24✔
1815

1816
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
24✔
1817
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
24✔
1818
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
24✔
1819
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
24✔
1820

1821
  if (pStmt->bindThreadInUse) {
24!
1822
    (void)taosThreadJoin(pStmt->bindThread, NULL);
24✔
1823
    pStmt->bindThreadInUse = false;
24✔
1824
  }
1825

1826
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
24!
1827
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
24!
1828
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1829
  }
1830
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
24!
1831

1832
  (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
24✔
1833
  (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
24✔
1834

1835
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
24✔
1836
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
24✔
1837

1838
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
24!
1839
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1840
      tscError("failed to wait asyncExecSem");
×
1841
    }
1842
  }
1843

1844
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
24!
1845
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1846
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1847
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1848
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1849
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1850
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1851
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1852
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1853
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1854

1855
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
24!
1856

1857
  if (pStmt->options.asyncExecFn) {
24!
1858
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
1859
      tscError("failed to destroy asyncExecSem");
×
1860
    }
1861
  }
1862
  taosMemoryFree(stmt);
24!
1863

1864
  return TSDB_CODE_SUCCESS;
24✔
1865
}
1866

1867
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
1868
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1869

1870
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
1871
    return (char*)tstrerror(terrno);
×
1872
  }
1873

1874
  pStmt->exec.pRequest->code = terrno;
×
1875

1876
  return taos_errstr(pStmt->exec.pRequest);
×
1877
}
1878
/*
1879
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
1880

1881
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
1882
*/
1883

1884
int stmtParseColFields2(TAOS_STMT2* stmt) {
×
1885
  int32_t    code = 0;
×
1886
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1887
  int32_t    preCode = pStmt->errCode;
×
1888

1889
  STMT_DLOG_E("start to get col fields");
×
1890

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

1895
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1896
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1897
  }
1898

1899
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1900

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

1906
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1907
    taos_free_result(pStmt->exec.pRequest);
×
1908
    pStmt->exec.pRequest = NULL;
×
1909
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1910
  }
1911

1912
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1913

1914
  if (pStmt->bInfo.needParse) {
×
1915
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1916
  }
1917

1918
_return:
×
1919

1920
  pStmt->errCode = preCode;
×
1921

1922
  return code;
×
1923
}
1924

1925
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
×
1926
  int32_t code = stmtParseColFields2(stmt);
×
1927
  if (code != TSDB_CODE_SUCCESS) {
×
1928
    return code;
×
1929
  }
1930

1931
  return stmtFetchStbColFields2(stmt, nums, fields);
×
1932
}
1933

1934
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
1935
  int32_t    code = 0;
×
1936
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1937
  int32_t    preCode = pStmt->errCode;
×
1938

1939
  STMT_DLOG_E("start to get param num");
×
1940

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

1945
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1946

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

1952
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1953
    taos_free_result(pStmt->exec.pRequest);
×
1954
    pStmt->exec.pRequest = NULL;
×
1955
  }
1956

1957
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1958

1959
  if (pStmt->bInfo.needParse) {
×
1960
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1961
  }
1962

1963
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1964
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1965
  } else {
1966
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
1967
  }
1968

1969
_return:
×
1970

1971
  pStmt->errCode = preCode;
×
1972

1973
  return code;
×
1974
}
1975

1976
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
×
1977
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1978

1979
  STMT_DLOG_E("start to use result");
×
1980

1981
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
1982
    tscError("useResult only for query statement");
×
1983
    return NULL;
×
1984
  }
1985

1986
  return pStmt->exec.pRequest;
×
1987
}
1988

1989
int32_t stmtAsyncBindThreadFunc(void* args) {
×
1990
  qInfo("async stmt bind thread started");
×
1991

1992
  ThreadArgs* targs = (ThreadArgs*)args;
×
1993
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
1994

1995
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
1996
  targs->fp(targs->param, NULL, code);
×
1997
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
1998
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
1999
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2000
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2001
  taosMemoryFree(args);
×
2002

2003
  qInfo("async stmt bind thread stopped");
×
2004

2005
  return code;
×
2006
}
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