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

taosdata / TDengine / #3818

01 Apr 2025 07:46AM UTC coverage: 34.065% (-0.02%) from 34.08%
#3818

push

travis-ci

happyguoxy
test:alter gcda dir

148531 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222425 of 489446 relevant lines covered (45.44%)

762721.74 hits per line

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

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

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
813✔
42
  int i = 0;
813✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
2,759✔
44
    if (i < 10) {
1,947✔
45
      taosUsleep(1);
1,883✔
46
      i++;
1,882✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
64✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
64!
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
64✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
64✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
814✔
56
    return false;
17✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
797✔
59
  SStmtQNode* node = pStmt->queue.head->next;
797✔
60
  pStmt->queue.head = pStmt->queue.head->next;
797✔
61
  *param = node;
797✔
62

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

65
  return true;
797✔
66
}
67

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

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

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

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

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

100
  return code;
1,180✔
101
}
102

103
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
2,313✔
104
  int32_t code = 0;
2,313✔
105

106
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
2,313!
107
    STMT_LOG_SEQ(newStatus);
2,313!
108
  }
109

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

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

174
  STMT_ERR_RET(code);
2,312✔
175

176
  pStmt->sql.status = newStatus;
2,311✔
177

178
  return TSDB_CODE_SUCCESS;
2,311✔
179
}
180

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

184
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
58✔
185

186
  if ('\0' == pStmt->bInfo.tbName[0]) {
58✔
187
    tscWarn("no table name set, OK if it is a stmt get fields");
24!
188
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
24!
189
  }
190

191
  *tbName = pStmt->bInfo.tbName;
34✔
192

193
  return TSDB_CODE_SUCCESS;
34✔
194
}
195

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

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

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

214
  if (!pStmt->bInfo.tagsCached) {
62!
215
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
62✔
216
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
62!
217
  }
218

219
  pStmt->bInfo.boundTags = tags;
62✔
220
  pStmt->bInfo.tagsCached = false;
62✔
221
  pStmt->bInfo.tbNameFlag = tbNameFlag;
62✔
222
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
62✔
223

224
  return TSDB_CODE_SUCCESS;
62✔
225
}
226

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

230
  pStmt->sql.pVgHash = pVgHash;
62✔
231
  pStmt->exec.pBlockHash = pBlockHash;
62✔
232

233
  return TSDB_CODE_SUCCESS;
62✔
234
}
235

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

240
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
62!
241
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
62!
242

243
  pStmt->sql.autoCreateTbl = autoCreateTbl;
62✔
244

245
  return TSDB_CODE_SUCCESS;
62✔
246
}
247

248
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
2✔
249
  STscStmt2* pStmt = (STscStmt2*)stmt;
2✔
250

251
  *pVgHash = pStmt->sql.pVgHash;
2✔
252
  pStmt->sql.pVgHash = NULL;
2✔
253

254
  *pBlockHash = pStmt->exec.pBlockHash;
2✔
255
  pStmt->exec.pBlockHash = NULL;
2✔
256

257
  return TSDB_CODE_SUCCESS;
2✔
258
}
259

260
static int32_t stmtParseSql(STscStmt2* pStmt) {
88✔
261
  pStmt->exec.pCurrBlock = NULL;
88✔
262

263
  SStmtCallback stmtCb = {
88✔
264
      .pStmt = pStmt,
265
      .getTbNameFn = stmtGetTbName,
266
      .setInfoFn = stmtUpdateInfo,
267
      .getExecInfoFn = stmtGetExecInfo,
268
  };
269

270
  STMT_ERR_RET(stmtCreateRequest(pStmt));
88!
271

272
  pStmt->stat.parseSqlNum++;
88✔
273
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
88✔
274
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
66✔
275

276
  pStmt->bInfo.needParse = false;
66✔
277

278
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
66✔
279
    pStmt->sql.type = STMT_TYPE_INSERT;
9✔
280
    pStmt->sql.stbInterlaceMode = false;
9✔
281
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
57✔
282
    pStmt->sql.type = STMT_TYPE_QUERY;
4✔
283
    pStmt->sql.stbInterlaceMode = false;
4✔
284

285
    return TSDB_CODE_SUCCESS;
4✔
286
  }
287

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

294
  STableDataCxt* pTableCtx = *pSrc;
62✔
295
  // if (pStmt->sql.stbInterlaceMode) {
296
  //   int16_t lastIdx = -1;
297

298
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
299
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
300
  //       pStmt->sql.stbInterlaceMode = false;
301
  //       break;
302
  //     }
303

304
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
305
  //   }
306
  // }
307

308
  if (NULL == pStmt->sql.pBindInfo) {
62✔
309
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
60!
310
    if (NULL == pStmt->sql.pBindInfo) {
60!
311
      return terrno;
×
312
    }
313
  }
314

315
  return TSDB_CODE_SUCCESS;
62✔
316
}
317

318
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
426✔
319
  pStmt->bInfo.tbUid = 0;
426✔
320
  pStmt->bInfo.tbVgId = -1;
426✔
321
  pStmt->bInfo.tbType = 0;
426✔
322
  pStmt->bInfo.needParse = true;
426✔
323
  pStmt->bInfo.inExecCache = false;
426✔
324

325
  pStmt->bInfo.tbName[0] = 0;
426✔
326
  pStmt->bInfo.tbFName[0] = 0;
426✔
327
  if (!pStmt->bInfo.tagsCached) {
426✔
328
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
358✔
329
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
358!
330
  }
331
  if (!pStmt->sql.autoCreateTbl) {
426✔
332
    pStmt->bInfo.stbFName[0] = 0;
356✔
333
    pStmt->bInfo.tbSuid = 0;
356✔
334
  }
335

336
  return TSDB_CODE_SUCCESS;
426✔
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) {
229✔
345
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
229✔
346
  if (NULL == pTblBuf->pCurBuff) {
229!
347
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
348
    return;
×
349
  }
350
  pTblBuf->buffIdx = 1;
229✔
351
  pTblBuf->buffOffset = sizeof(*pQueue->head);
229✔
352

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

358
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
364✔
359
  if (pStmt->sql.stbInterlaceMode) {
364✔
360
    if (deepClean) {
249✔
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);
16!
366
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
16✔
367
      }
368
    } else {
369
      pStmt->sql.siInfo.pTableColsIdx = 0;
229✔
370
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
229✔
371
    }
372
    if (NULL != pStmt->exec.pRequest) {
249✔
373
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
248✔
374
    }
375
  } else {
376
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
115✔
377
      // if (!pStmt->options.asyncExecFn) {
378
      taos_free_result(pStmt->exec.pRequest);
112✔
379
      pStmt->exec.pRequest = NULL;
112✔
380
      //}
381
    }
382

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

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

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

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

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

404
    if (keepTable) {
115✔
405
      return TSDB_CODE_SUCCESS;
46✔
406
    }
407

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

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

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

417
  return TSDB_CODE_SUCCESS;
318✔
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) {
17,000✔
426
  SArray* pCols = *(SArray**)buf;
17,000✔
427
  taosArrayDestroy(pCols);
17,000✔
428
}
17,000✔
429

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

433
  taosMemoryFreeClear(pStmt->db);
89!
434
  taosMemoryFree(pStmt->sql.pBindInfo);
89!
435
  taosMemoryFree(pStmt->sql.queryRes.fields);
89!
436
  taosMemoryFree(pStmt->sql.queryRes.userFields);
89!
437
  taosMemoryFree(pStmt->sql.sqlStr);
89!
438
  qDestroyQuery(pStmt->sql.pQuery);
89✔
439
  taosArrayDestroy(pStmt->sql.nodeList);
89✔
440
  taosHashCleanup(pStmt->sql.pVgHash);
89✔
441
  pStmt->sql.pVgHash = NULL;
89✔
442
  if (pStmt->sql.fixValueTags) {
89✔
443
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
3!
444
  }
445

446
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
89✔
447
  while (pIter) {
105✔
448
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
16✔
449

450
    qDestroyStmtDataBlock(pCache->pDataCtx);
16✔
451
    qDestroyBoundColInfo(pCache->boundTags);
16✔
452
    taosMemoryFreeClear(pCache->boundTags);
16!
453

454
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
16✔
455
  }
456
  taosHashCleanup(pStmt->sql.pTableCache);
89✔
457
  pStmt->sql.pTableCache = NULL;
89✔
458

459
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
89!
460
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
89!
461

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

470
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
89✔
471
  pStmt->sql.siInfo.tableColsReady = true;
89✔
472

473
  STMT_DLOG_E("end to free SQL info");
89!
474

475
  return TSDB_CODE_SUCCESS;
89✔
476
}
477

478
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
126✔
479
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
126✔
480
    return TSDB_CODE_SUCCESS;
15✔
481
  }
482

483
  SVgroupInfo      vgInfo = {0};
111✔
484
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
111✔
485
                           .requestId = pStmt->exec.pRequest->requestId,
111✔
486
                           .requestObjRefId = pStmt->exec.pRequest->self,
111✔
487
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
111✔
488

489
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
111✔
490
  if (TSDB_CODE_SUCCESS != code) {
111!
491
    return code;
×
492
  }
493

494
  code =
495
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
111✔
496
  if (TSDB_CODE_SUCCESS != code) {
111!
497
    return code;
×
498
  }
499

500
  *vgId = vgInfo.vgId;
111✔
501

502
  return TSDB_CODE_SUCCESS;
111✔
503
}
504

505
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
81✔
506
                                    uint64_t suid, int32_t vgId) {
507
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
81!
508
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
81!
509

510
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
81!
511

512
  return TSDB_CODE_SUCCESS;
81✔
513
}
514

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

522
  pStmt->bInfo.needParse = true;
139✔
523
  pStmt->bInfo.inExecCache = false;
139✔
524

525
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
139✔
526
  if (pCxtInExec) {
139✔
527
    pStmt->bInfo.needParse = false;
24✔
528
    pStmt->bInfo.inExecCache = true;
24✔
529

530
    pStmt->exec.pCurrBlock = *pCxtInExec;
24✔
531

532
    if (pStmt->sql.autoCreateTbl) {
24✔
533
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
18!
534
      return TSDB_CODE_SUCCESS;
18✔
535
    }
536
  }
537

538
  if (NULL == pStmt->pCatalog) {
121✔
539
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
34!
540
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
34✔
541
  }
542

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

550
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
34!
551
    return TSDB_CODE_SUCCESS;
34✔
552
  }
553

554
  if (pStmt->sql.autoCreateTbl) {
87✔
555
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
63✔
556
    if (pCache) {
63!
557
      pStmt->bInfo.needParse = false;
63✔
558
      pStmt->bInfo.tbUid = 0;
63✔
559

560
      STableDataCxt* pNewBlock = NULL;
63✔
561
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
63!
562

563
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
63!
564
                      POINTER_BYTES)) {
565
        STMT_ERR_RET(terrno);
×
566
      }
567

568
      pStmt->exec.pCurrBlock = pNewBlock;
63✔
569

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

572
      return TSDB_CODE_SUCCESS;
63✔
573
    }
574

575
    STMT_RET(stmtCleanBindInfo(pStmt));
×
576
  }
577

578
  uint64_t uid, suid;
579
  int32_t  vgId;
580
  int8_t   tableType;
581

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

589
  pStmt->stat.ctgGetTbMetaNum++;
24✔
590

591
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
24!
592
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
593
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
594

595
    STMT_ERR_RET(code);
×
596
  }
597

598
  STMT_ERR_RET(code);
24!
599

600
  uid = pTableMeta->uid;
24✔
601
  suid = pTableMeta->suid;
24✔
602
  tableType = pTableMeta->tableType;
24✔
603
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
24✔
604
  vgId = pTableMeta->vgId;
24✔
605

606
  taosMemoryFree(pTableMeta);
24!
607

608
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
24!
609

610
  if (uid == pStmt->bInfo.tbUid) {
24!
611
    pStmt->bInfo.needParse = false;
×
612

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

615
    return TSDB_CODE_SUCCESS;
×
616
  }
617

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

624
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
625
    }
626

627
    pStmt->bInfo.needParse = false;
6✔
628

629
    pStmt->bInfo.tbUid = uid;
6✔
630
    pStmt->bInfo.tbSuid = suid;
6✔
631
    pStmt->bInfo.tbType = tableType;
6✔
632
    pStmt->bInfo.boundTags = pCache->boundTags;
6✔
633
    pStmt->bInfo.tagsCached = true;
6✔
634

635
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
6!
636

637
    return TSDB_CODE_SUCCESS;
6✔
638
  }
639

640
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
18✔
641
  if (pCache) {
18!
642
    pStmt->bInfo.needParse = false;
18✔
643

644
    pStmt->bInfo.tbUid = uid;
18✔
645
    pStmt->bInfo.tbSuid = suid;
18✔
646
    pStmt->bInfo.tbType = tableType;
18✔
647
    pStmt->bInfo.boundTags = pCache->boundTags;
18✔
648
    pStmt->bInfo.tagsCached = true;
18✔
649

650
    STableDataCxt* pNewBlock = NULL;
18✔
651
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
18!
652

653
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
18!
654
                    POINTER_BYTES)) {
655
      STMT_ERR_RET(terrno);
×
656
    }
657

658
    pStmt->exec.pCurrBlock = pNewBlock;
18✔
659

660
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
18!
661

662
    return TSDB_CODE_SUCCESS;
18✔
663
  }
664

665
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
666

667
  return TSDB_CODE_SUCCESS;
×
668
}
669

670
static int32_t stmtResetStmt(STscStmt2* pStmt) {
1✔
671
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
1!
672

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

678
  pStmt->sql.status = STMT_INIT;
1✔
679

680
  return TSDB_CODE_SUCCESS;
1✔
681
}
682

683
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
796✔
684
  SStmtQNode* pParam = (SStmtQNode*)param;
796✔
685

686
  if (pParam->restoreTbCols) {
796✔
687
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
797✔
688
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
568✔
689
      *p = taosArrayInit(20, POINTER_BYTES);
568✔
690
      if (*p == NULL) {
568!
691
        STMT_ERR_RET(terrno);
×
692
      }
693
    }
694

695
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
229✔
696
  } else {
697
    int code = qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
567✔
698
                                      &pStmt->sql.siInfo, pParam->pCreateTbReq);
699
    // taosMemoryFree(pParam->pTbData);
700
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
567✔
701
    STMT_ERR_RET(code);
568!
702
  }
703
  return TSDB_CODE_SUCCESS;
797✔
704
}
705

706
static void* stmtBindThreadFunc(void* param) {
24✔
707
  setThreadName("stmtBind");
24✔
708

709
  qInfo("stmt bind thread started");
24!
710

711
  STscStmt2* pStmt = (STscStmt2*)param;
24✔
712

713
  while (true) {
814✔
714
    if (pStmt->queue.stopQueue) {
838✔
715
      break;
24✔
716
    }
717

718
    SStmtQNode* asyncParam = NULL;
814✔
719
    if (!stmtDequeue(pStmt, &asyncParam)) {
814✔
720
      continue;
17✔
721
    }
722

723
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
797!
724
      qError("stmt async output failed");
×
725
    }
726
  }
727

728
  qInfo("stmt bind thread stopped");
24!
729

730
  return NULL;
24✔
731
}
732

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

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

747
  pStmt->bindThreadInUse = true;
24✔
748

749
  (void)taosThreadAttrDestroy(&thAttr);
24✔
750
  return TSDB_CODE_SUCCESS;
24✔
751
}
752

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

759
  return TSDB_CODE_SUCCESS;
24✔
760
}
761

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

767
  return TSDB_CODE_SUCCESS;
90✔
768
}
769

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

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

786
  pTblBuf->pCurBuff = buff;
24✔
787
  pTblBuf->buffIdx = 1;
24✔
788
  pTblBuf->buffOffset = 0;
24✔
789

790
  return TSDB_CODE_SUCCESS;
24✔
791
}
792

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

798
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
90!
799
  if (NULL == pStmt) {
90!
800
    return NULL;
×
801
  }
802

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

809
  pStmt->taos = pObj;
90✔
810
  pStmt->bInfo.needParse = true;
90✔
811
  pStmt->sql.status = STMT_INIT;
90✔
812
  pStmt->errCode = TSDB_CODE_SUCCESS;
90✔
813

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

820
    pStmt->reqid = pOptions->reqid;
89✔
821
  }
822

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

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

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

869
  pStmt->execSemWaited = false;
90✔
870

871
  STMT_LOG_SEQ(STMT_INIT);
90!
872

873
  tscDebug("stmt:%p initialized", pStmt);
90!
874

875
  return pStmt;
90✔
876
}
877

878
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
52✔
879
  STscStmt2* pStmt = (STscStmt2*)stmt;
52✔
880

881
  STMT_DLOG("start to set dbName:%s", dbName);
52!
882

883
  pStmt->db = taosStrdup(dbName);
52!
884
  (void)strdequote(pStmt->db);
52✔
885
  STMT_ERR_RET(stmtCreateRequest(pStmt));
52!
886

887
  // The SQL statement specifies a database name, overriding the previously specified database
888
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
52!
889
  pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
52!
890
  if (pStmt->exec.pRequest->pDb == NULL) {
52!
891
    return terrno;
×
892
  }
893
  if (pStmt->sql.stbInterlaceMode) {
52✔
894
    pStmt->sql.siInfo.dbname = pStmt->db;
10✔
895
  }
896
  return TSDB_CODE_SUCCESS;
52✔
897
}
898

899
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
89✔
900
  STscStmt2* pStmt = (STscStmt2*)stmt;
89✔
901

902
  STMT_DLOG_E("start to prepare");
89!
903

904
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
89✔
905
    return pStmt->errCode;
1✔
906
  }
907

908
  if (pStmt->sql.status >= STMT_PREPARE) {
88✔
909
    STMT_ERR_RET(stmtResetStmt(pStmt));
1!
910
  }
911

912
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
88!
913

914
  if (length <= 0) {
88✔
915
    length = strlen(sql);
83✔
916
  }
917

918
  pStmt->sql.sqlStr = taosStrndup(sql, length);
88!
919
  if (!pStmt->sql.sqlStr) {
88!
920
    return terrno;
×
921
  }
922
  pStmt->sql.sqlLen = length;
88✔
923
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
88✔
924

925
  char* dbName = NULL;
88✔
926
  if (qParseDbName(sql, length, &dbName)) {
88✔
927
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
52!
928
    taosMemoryFreeClear(dbName);
52!
929
  }
930

931
  return TSDB_CODE_SUCCESS;
88✔
932
}
933

934
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
17✔
935
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
17✔
936
  if (!pSrc) {
17!
937
    return terrno;
×
938
  }
939
  STableDataCxt* pDst = NULL;
17✔
940

941
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
17!
942
  pStmt->sql.siInfo.pDataCtx = pDst;
17✔
943

944
  SArray* pTblCols = NULL;
17✔
945
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
16,904✔
946
    pTblCols = taosArrayInit(20, POINTER_BYTES);
16,887✔
947
    if (NULL == pTblCols) {
16,857!
948
      return terrno;
×
949
    }
950

951
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
33,744!
952
      return terrno;
×
953
    }
954
  }
955

956
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
17✔
957

958
  return TSDB_CODE_SUCCESS;
17✔
959
}
960

961
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
1,444✔
962
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,444✔
963

964
  STMT_DLOG_E("start is insert");
1,444!
965

966
  if (pStmt->sql.type) {
1,444✔
967
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,356✔
968
  } else {
969
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
88✔
970
  }
971

972
  return TSDB_CODE_SUCCESS;
1,444✔
973
}
974

975
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
693✔
976
  STscStmt2* pStmt = (STscStmt2*)stmt;
693✔
977

978
  int64_t startUs = taosGetTimestampUs();
693✔
979

980
  STMT_DLOG("start to set tbName:%s", tbName);
693!
981

982
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
693!
983
    return pStmt->errCode;
×
984
  }
985

986
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
693!
987

988
  int32_t insert = 0;
692✔
989
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
692!
990
  if (0 == insert) {
692!
991
    tscError("set tb name not available for none insert statement");
×
992
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
993
  }
994

995
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
692✔
996
    STMT_ERR_RET(stmtCreateRequest(pStmt));
140!
997

998
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
140!
999
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1000
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
140✔
1001

1002
    STMT_ERR_RET(stmtGetFromCache(pStmt));
139!
1003

1004
    if (pStmt->bInfo.needParse) {
139✔
1005
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
34✔
1006
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
34✔
1007

1008
      STMT_ERR_RET(stmtParseSql(pStmt));
34!
1009
    }
1010
  } else {
1011
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
552✔
1012
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
552✔
1013
    pStmt->exec.pRequest->requestId++;
552✔
1014
    pStmt->bInfo.needParse = false;
552✔
1015
  }
1016

1017
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
691✔
1018
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
17!
1019
  }
1020

1021
  int64_t startUs2 = taosGetTimestampUs();
692✔
1022
  pStmt->stat.setTbNameUs += startUs2 - startUs;
692✔
1023

1024
  return TSDB_CODE_SUCCESS;
692✔
1025
}
1026

1027
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
132✔
1028
  STscStmt2* pStmt = (STscStmt2*)stmt;
132✔
1029

1030
  STMT_DLOG_E("start to set tbTags");
132!
1031

1032
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
132!
1033
    return pStmt->errCode;
×
1034
  }
1035

1036
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
132!
1037

1038
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
132!
1039
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1040
    pStmt->bInfo.needParse = false;
×
1041
  }
1042
  STMT_ERR_RET(stmtCreateRequest(pStmt));
132!
1043

1044
  if (pStmt->bInfo.needParse) {
132!
1045
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1046
  }
1047
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
132!
1048
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1049
  }
1050

1051
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
132✔
1052
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1053
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1054
  //   return TSDB_CODE_SUCCESS;
1055
  // }
1056
  if (pStmt->sql.autoCreateTbl && pStmt->sql.stbInterlaceMode) {
132!
1057
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
37!
1058
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1059
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
37!
1060
  }
1061

1062
  STableDataCxt** pDataBlock = NULL;
132✔
1063
  if (pStmt->exec.pCurrBlock) {
132✔
1064
    pDataBlock = &pStmt->exec.pCurrBlock;
112✔
1065
  } else {
1066
    pDataBlock =
1067
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20✔
1068
    if (NULL == pDataBlock) {
20!
1069
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1070
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1071
    }
1072
    // pStmt->exec.pCurrBlock = *pDataBlock;
1073
    // if (pStmt->sql.stbInterlaceMode) {
1074
    //   taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
1075
    //   (*pDataBlock)->pData->aCol = NULL;
1076
    // }
1077
  }
1078
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
132!
1079
    return TSDB_CODE_SUCCESS;
×
1080
  }
1081

1082
  tscDebug("start to bind stmt tag values");
132!
1083

1084
  void* boundTags = NULL;
132✔
1085
  if (pStmt->sql.stbInterlaceMode) {
132✔
1086
    boundTags = pStmt->sql.siInfo.boundTags;
37✔
1087
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
37!
1088
    if (NULL == pCreateTbReq) {
37!
1089
      return terrno;
×
1090
    }
1091
    int32_t vgId = -1;
37✔
1092
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
37!
1093
    (*pCreateTbReq)->uid = vgId;
37✔
1094
  } else {
1095
    boundTags = pStmt->bInfo.boundTags;
95✔
1096
  }
1097

1098
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
132✔
1099
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1100
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1101

1102
  return TSDB_CODE_SUCCESS;
131✔
1103
}
1104

1105
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
11✔
1106
  STscStmt2* pStmt = (STscStmt2*)stmt;
11✔
1107

1108
  STMT_DLOG_E("start to set tbTags");
11!
1109

1110
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11!
1111
    return pStmt->errCode;
×
1112
  }
1113

1114
  if (!pStmt->sql.stbInterlaceMode) {
11!
1115
    return TSDB_CODE_SUCCESS;
×
1116
  }
1117

1118
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
11!
1119

1120
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
11!
1121
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1122
    pStmt->bInfo.needParse = false;
×
1123
  }
1124
  STMT_ERR_RET(stmtCreateRequest(pStmt));
11!
1125

1126
  if (pStmt->bInfo.needParse) {
11!
1127
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1128
    if (!pStmt->sql.autoCreateTbl) {
×
1129
      return TSDB_CODE_SUCCESS;
×
1130
    }
1131
  }
1132

1133
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
11!
1134
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1135
  }
1136

1137
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
11!
1138
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1139
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
11!
1140

1141
  STableDataCxt** pDataBlock = NULL;
11✔
1142
  if (pStmt->exec.pCurrBlock) {
11✔
1143
    pDataBlock = &pStmt->exec.pCurrBlock;
8✔
1144
  } else {
1145
    pDataBlock =
1146
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3✔
1147
    if (NULL == pDataBlock) {
3!
1148
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1149
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1150
    }
1151
  }
1152

1153
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
11!
1154
    return TSDB_CODE_SUCCESS;
×
1155
  }
1156

1157
  if (pStmt->sql.fixValueTags) {
11✔
1158
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
8!
1159
    if ((*pCreateTbReq)->name) {
8!
1160
      taosMemoryFree((*pCreateTbReq)->name);
8!
1161
    }
1162
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
8!
1163
    int32_t vgId = -1;
8✔
1164
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
8!
1165
    (*pCreateTbReq)->uid = vgId;
8✔
1166
    return TSDB_CODE_SUCCESS;
8✔
1167
  }
1168

1169
  if ((*pDataBlock)->pData->pCreateTbReq) {
3!
1170
    pStmt->sql.fixValueTags = true;
3✔
1171
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
3!
1172
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
3!
1173
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
3✔
1174
  }
1175

1176
  return TSDB_CODE_SUCCESS;
3✔
1177
}
1178

1179
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1180
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1181
    return pStmt->errCode;
×
1182
  }
1183

1184
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1185
    tscError("invalid operation to get query column fileds");
×
1186
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1187
  }
1188

1189
  STableDataCxt** pDataBlock = NULL;
×
1190

1191
  if (pStmt->sql.stbInterlaceMode) {
×
1192
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1193
  } else {
1194
    pDataBlock =
1195
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1196
    if (NULL == pDataBlock) {
×
1197
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1198
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1199
    }
1200
  }
1201

1202
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1203

1204
  return TSDB_CODE_SUCCESS;
×
1205
}
1206

1207
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
33✔
1208
  int32_t    code = 0;
33✔
1209
  int32_t    preCode = pStmt->errCode;
33✔
1210

1211
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
33!
1212
    return pStmt->errCode;
×
1213
  }
1214

1215
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
33!
1216
    tscError("invalid operation to get query column fileds");
×
1217
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1218
  }
1219

1220
  STableDataCxt** pDataBlock = NULL;
33✔
1221
  bool            cleanStb = false;
33✔
1222

1223
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
33✔
1224
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
7✔
1225
  } else {
1226
    cleanStb = true;
26✔
1227
    pDataBlock =
1228
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
26✔
1229
  }
1230

1231
  if (NULL == pDataBlock || NULL == *pDataBlock) {
33!
1232
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1233
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1234
  }
1235

1236
  STMT_ERRI_JRET(
33!
1237
      qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbNameFlag, fieldNum, fields));
1238

1239
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
33!
1240
    qDestroyStmtDataBlock(*pDataBlock);
19✔
1241
    *pDataBlock = NULL;
19✔
1242
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
19!
1243
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
1244
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1245
    }
1246
    pStmt->sql.autoCreateTbl = false;
19✔
1247
    pStmt->bInfo.tagsCached = false;
19✔
1248
    pStmt->bInfo.sname = (SName){0};
19✔
1249
    stmtCleanBindInfo(pStmt);
19✔
1250
  }
1251

1252
_return:
14✔
1253

1254
  pStmt->errCode = preCode;
33✔
1255

1256
  return code;
33✔
1257
}
1258
/*
1259
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1260
  while (true) {
1261
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1262
      pStmt->exec.smInfo.pColIdx = 0;
1263
    }
1264

1265
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1266
      taosUsleep(1);
1267
      continue;
1268
    }
1269

1270
    *idx = pStmt->exec.smInfo.pColIdx;
1271
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1272
  }
1273
}
1274
*/
1275
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
567✔
1276
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
567✔
1277
    pStmt->sql.siInfo.pVgroupHash =
229✔
1278
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
229✔
1279
  }
1280
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
567✔
1281
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
229✔
1282
  }
1283

1284
  if (NULL == pStmt->sql.siInfo.pRequest) {
566✔
1285
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
15!
1286
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1287

1288
    if (pStmt->reqid != 0) {
15!
1289
      pStmt->reqid++;
×
1290
    }
1291
    pStmt->exec.pRequest->syncQuery = true;
15✔
1292

1293
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
15✔
1294
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
15✔
1295
  }
1296

1297
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
566✔
1298
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
26✔
1299
    pStmt->sql.siInfo.tbFromHash = true;
10✔
1300
  }
1301

1302
  if (0 == pStmt->sql.siInfo.firstName[0]) {
566✔
1303
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
15✔
1304
  }
1305

1306
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
566✔
1307
  param->next = NULL;
566✔
1308

1309
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
566✔
1310

1311
  stmtEnqueue(pStmt, param);
568✔
1312

1313
  return TSDB_CODE_SUCCESS;
568✔
1314
}
1315

1316
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1317
  while (true) {
1318
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
569!
1319
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
569✔
1320
      break;
569✔
1321
    } else {
1322
      SArray* pTblCols = NULL;
×
1323
      for (int32_t i = 0; i < 100; i++) {
×
1324
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1325
        if (NULL == pTblCols) {
×
1326
          return terrno;
×
1327
        }
1328

1329
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1330
          return terrno;
×
1331
        }
1332
      }
1333
    }
1334
  }
1335

1336
  return TSDB_CODE_SUCCESS;
569✔
1337
}
1338

1339
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
127✔
1340
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
127✔
1341
    return TSDB_CODE_SUCCESS;
7✔
1342
  }
1343

1344
  uint64_t uid = pStmt->bInfo.tbUid;
120✔
1345
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
120!
1346

1347
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
120✔
1348
    return TSDB_CODE_SUCCESS;
104✔
1349
  }
1350

1351
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
16✔
1352
  if (!pSrc) {
16!
1353
    return terrno;
×
1354
  }
1355
  STableDataCxt* pDst = NULL;
16✔
1356

1357
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
16!
1358

1359
  SStmtTableCache cache = {
16✔
1360
      .pDataCtx = pDst,
1361
      .boundTags = pStmt->bInfo.boundTags,
16✔
1362
  };
1363

1364
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
16!
1365
    return terrno;
×
1366
  }
1367

1368
  if (pStmt->sql.autoCreateTbl) {
16✔
1369
    pStmt->bInfo.tagsCached = true;
13✔
1370
  } else {
1371
    pStmt->bInfo.boundTags = NULL;
3✔
1372
  }
1373

1374
  return TSDB_CODE_SUCCESS;
16✔
1375
}
1376

1377
static int stmtAddBatch2(TAOS_STMT2* stmt) {
356✔
1378
  STscStmt2* pStmt = (STscStmt2*)stmt;
356✔
1379

1380
  int64_t startUs = taosGetTimestampUs();
356✔
1381

1382
  STMT_DLOG_E("start to add batch");
356!
1383

1384
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
356!
1385
    return pStmt->errCode;
×
1386
  }
1387

1388
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
356!
1389

1390
  if (pStmt->sql.stbInterlaceMode) {
356✔
1391
    int64_t startUs2 = taosGetTimestampUs();
229✔
1392
    pStmt->stat.addBatchUs += startUs2 - startUs;
229✔
1393

1394
    pStmt->sql.siInfo.tableColsReady = false;
229✔
1395

1396
    SStmtQNode* param = NULL;
229✔
1397
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
458!
1398
    param->restoreTbCols = true;
229✔
1399
    param->next = NULL;
229✔
1400

1401
    if (pStmt->sql.autoCreateTbl) {
229✔
1402
      pStmt->bInfo.tagsCached = true;
20✔
1403
    }
1404

1405
    stmtEnqueue(pStmt, param);
229✔
1406

1407
    return TSDB_CODE_SUCCESS;
229✔
1408
  }
1409

1410
  STMT_ERR_RET(stmtCacheBlock(pStmt));
127!
1411

1412
  return TSDB_CODE_SUCCESS;
127✔
1413
}
1414
/*
1415
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1416
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1417
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1418
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1419

1420
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1421
  pRes->fields = taosMemoryMalloc(size);
1422
  pRes->userFields = taosMemoryMalloc(size);
1423
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1424
    STMT_ERR_RET(terrno);
1425
  }
1426
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1427
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1428

1429
  return TSDB_CODE_SUCCESS;
1430
}
1431

1432
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1433
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1434
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1435

1436
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1437
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1438

1439
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1440
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1441
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1442
      STMT_ERR_RET(terrno);
1443
    }
1444
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1445
  }
1446

1447
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1448
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1449
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1450
      STMT_ERR_RET(terrno);
1451
    }
1452
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1453
  }
1454

1455
  return TSDB_CODE_SUCCESS;
1456
}
1457
*/
1458
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
700✔
1459
  STscStmt2* pStmt = (STscStmt2*)stmt;
700✔
1460
  int32_t    code = 0;
700✔
1461

1462
  int64_t startUs = taosGetTimestampUs();
700✔
1463

1464
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
700!
1465

1466
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
700!
1467
    return pStmt->errCode;
×
1468
  }
1469

1470
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
700!
1471

1472
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
700!
1473
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1474
    pStmt->bInfo.needParse = false;
×
1475
  }
1476

1477
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
700!
1478
    taos_free_result(pStmt->exec.pRequest);
1✔
1479
    pStmt->exec.pRequest = NULL;
1✔
1480
  }
1481

1482
  STMT_ERR_RET(stmtCreateRequest(pStmt));
700!
1483

1484
  if (pStmt->bInfo.needParse) {
700✔
1485
    STMT_ERR_RET(stmtParseSql(pStmt));
4!
1486
  }
1487

1488
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
700✔
1489
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
3!
1490

1491
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
3✔
1492
                         .acctId = pStmt->taos->acctId,
3✔
1493
                         .db = pStmt->exec.pRequest->pDb,
3✔
1494
                         .topicQuery = false,
1495
                         .pSql = pStmt->sql.sqlStr,
3✔
1496
                         .sqlLen = pStmt->sql.sqlLen,
3✔
1497
                         .pMsg = pStmt->exec.pRequest->msgBuf,
3✔
1498
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1499
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
3✔
1500
                         .pStmtCb = NULL,
1501
                         .pUser = pStmt->taos->user};
3✔
1502
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
3✔
1503
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
3!
1504

1505
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
3!
1506

1507
    if (pStmt->sql.pQuery->haveResultSet) {
3!
1508
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
3!
1509
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1510
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
3!
1511
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
3!
1512
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
3✔
1513
    }
1514

1515
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
3✔
1516
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
3✔
1517
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
3✔
1518

1519
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1520
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1521
    // }
1522

1523
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1524

1525
    return TSDB_CODE_SUCCESS;
3✔
1526
  }
1527

1528
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
697!
1529
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1530
  }
1531

1532
  STableDataCxt** pDataBlock = NULL;
697✔
1533

1534
  if (pStmt->exec.pCurrBlock) {
697✔
1535
    pDataBlock = &pStmt->exec.pCurrBlock;
663✔
1536
  } else {
1537
    pDataBlock =
1538
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
34✔
1539
    if (NULL == pDataBlock) {
34!
1540
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1541
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1542
    }
1543
    pStmt->exec.pCurrBlock = *pDataBlock;
34✔
1544
    if (pStmt->sql.stbInterlaceMode) {
34✔
1545
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
16✔
1546
      (*pDataBlock)->pData->aCol = NULL;
16✔
1547
    }
1548
    if (colIdx < -1) {
34✔
1549
      pStmt->sql.bindRowFormat = true;
1✔
1550
      taosArrayDestroy((*pDataBlock)->pData->aCol);
1✔
1551
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
1✔
1552
    }
1553
  }
1554

1555
  int64_t startUs2 = taosGetTimestampUs();
697✔
1556
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
697✔
1557

1558
  SStmtQNode* param = NULL;
697✔
1559
  if (pStmt->sql.stbInterlaceMode) {
697✔
1560
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,138!
1561
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
1,138!
1562
    taosArrayClear(param->tblData.aCol);
569✔
1563

1564
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1565

1566
    param->restoreTbCols = false;
569✔
1567
    param->tblData.isOrdered = true;
569✔
1568
    param->tblData.isDuplicateTs = false;
569✔
1569
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
569✔
1570

1571
    param->pCreateTbReq = pCreateTbReq;
569✔
1572
  }
1573

1574
  int64_t startUs3 = taosGetTimestampUs();
697✔
1575
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
697✔
1576

1577
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
697✔
1578

1579
  if (colIdx < 0) {
697✔
1580
    if (pStmt->sql.stbInterlaceMode) {
691✔
1581
      // (*pDataBlock)->pData->flags = 0;
1582
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
569✔
1583
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
569✔
1584
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
569✔
1585
                                    pStmt->taos->optionInfo.charsetCxt);
569✔
1586
      param->tblData.isOrdered = (*pDataBlock)->ordered;
569✔
1587
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
569✔
1588
    } else {
1589
      if (colIdx == -1) {
122✔
1590
        if (pStmt->sql.bindRowFormat) {
120✔
1591
          tscError("can't mix bind row format and bind column format");
1!
1592
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
1!
1593
        }
1594
        code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
119✔
1595
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
119✔
1596
      } else {
1597
        code = qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, bind, pStmt->exec.pRequest->msgBuf,
2✔
1598
                                  pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
2✔
1599
                                  pStmt->taos->optionInfo.charsetCxt);
2✔
1600
      }
1601
    }
1602

1603
    if (code) {
690✔
1604
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1605
      STMT_ERR_RET(code);
1!
1606
    }
1607
  } else {
1608
    if (pStmt->sql.stbInterlaceMode) {
6!
1609
      tscError("bind single column not allowed in stb insert mode");
×
1610
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1611
    }
1612

1613
    if (pStmt->sql.bindRowFormat) {
6!
1614
      tscError("can't mix bind row format and bind column format");
×
1615
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1616
    }
1617

1618
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
6!
1619
      tscError("bind column index not in sequence");
×
1620
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1621
    }
1622

1623
    pStmt->bInfo.sBindLastIdx = colIdx;
6✔
1624

1625
    if (0 == colIdx) {
6✔
1626
      pStmt->bInfo.sBindRowNum = bind->num;
3✔
1627
    }
1628

1629
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
6✔
1630
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
6✔
1631
    if (code) {
6!
1632
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1633
      STMT_ERR_RET(code);
×
1634
    }
1635
  }
1636

1637
  int64_t startUs4 = taosGetTimestampUs();
694✔
1638
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
694✔
1639

1640
  if (pStmt->sql.stbInterlaceMode) {
694✔
1641
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
568!
1642
  } else {
1643
    STMT_ERR_RET(stmtAddBatch2(pStmt));
126!
1644
  }
1645

1646
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
695✔
1647

1648
  return TSDB_CODE_SUCCESS;
695✔
1649
}
1650
/*
1651
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1652
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1653

1654
  int32_t code = 0;
1655
  int32_t finalCode = 0;
1656
  size_t  keyLen = 0;
1657
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1658
  while (pIter) {
1659
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1660
    char*          key = taosHashGetKey(pIter, &keyLen);
1661

1662
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1663
    if (pMeta->uid) {
1664
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1665
      continue;
1666
    }
1667

1668
    SSubmitBlkRsp* blkRsp = NULL;
1669
    int32_t        i = 0;
1670
    for (; i < pRsp->nBlocks; ++i) {
1671
      blkRsp = pRsp->pBlocks + i;
1672
      if (strlen(blkRsp->tblFName) != keyLen) {
1673
        continue;
1674
      }
1675

1676
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1677
        continue;
1678
      }
1679

1680
      break;
1681
    }
1682

1683
    if (i < pRsp->nBlocks) {
1684
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1685
               blkRsp->uid);
1686

1687
      pMeta->uid = blkRsp->uid;
1688
      pStmt->bInfo.tbUid = blkRsp->uid;
1689
    } else {
1690
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1691
      if (NULL == pStmt->pCatalog) {
1692
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1693
        if (code) {
1694
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1695
          finalCode = code;
1696
          continue;
1697
        }
1698
      }
1699

1700
      code = stmtCreateRequest(pStmt);
1701
      if (code) {
1702
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1703
        finalCode = code;
1704
        continue;
1705
      }
1706

1707
      STableMeta*      pTableMeta = NULL;
1708
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1709
                               .requestId = pStmt->exec.pRequest->requestId,
1710
                               .requestObjRefId = pStmt->exec.pRequest->self,
1711
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1712
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1713

1714
      pStmt->stat.ctgGetTbMetaNum++;
1715

1716
      taos_free_result(pStmt->exec.pRequest);
1717
      pStmt->exec.pRequest = NULL;
1718

1719
      if (code || NULL == pTableMeta) {
1720
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1721
        finalCode = code;
1722
        taosMemoryFree(pTableMeta);
1723
        continue;
1724
      }
1725

1726
      pMeta->uid = pTableMeta->uid;
1727
      pStmt->bInfo.tbUid = pTableMeta->uid;
1728
      taosMemoryFree(pTableMeta);
1729
    }
1730

1731
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1732
  }
1733

1734
  return finalCode;
1735
}
1736
*/
1737
/*
1738
int stmtStaticModeExec(TAOS_STMT* stmt) {
1739
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1740
  int32_t     code = 0;
1741
  SSubmitRsp* pRsp = NULL;
1742
  if (pStmt->sql.staticMode) {
1743
    return TSDB_CODE_TSC_STMT_API_ERROR;
1744
  }
1745

1746
  STMT_DLOG_E("start to exec");
1747

1748
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1749

1750
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1751
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1752

1753
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1754

1755
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1756
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1757
    if (code) {
1758
      pStmt->exec.pRequest->code = code;
1759
    } else {
1760
      tFreeSSubmitRsp(pRsp);
1761
      STMT_ERR_RET(stmtResetStmt(pStmt));
1762
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1763
    }
1764
  }
1765

1766
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1767

1768
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1769
  pStmt->affectedRows += pStmt->exec.affectedRows;
1770

1771
_return:
1772

1773
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1774

1775
  tFreeSSubmitRsp(pRsp);
1776

1777
  ++pStmt->sql.runTimes;
1778

1779
  STMT_RET(code);
1780
}
1781
*/
1782

1783
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
12✔
1784
  const STscObj* pTscObj = pRequest->pTscObj;
12✔
1785

1786
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
12!
1787
  if (*pCxt == NULL) {
12!
1788
    return terrno;
×
1789
  }
1790

1791
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
12✔
1792
                           .requestRid = pRequest->self,
12✔
1793
                           .acctId = pTscObj->acctId,
12✔
1794
                           .db = pRequest->pDb,
12✔
1795
                           .topicQuery = false,
1796
                           .pSql = pRequest->sqlstr,
12✔
1797
                           .sqlLen = pRequest->sqlLen,
12✔
1798
                           .pMsg = pRequest->msgBuf,
12✔
1799
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1800
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
12✔
1801
                           .pStmtCb = NULL,
1802
                           .pUser = pTscObj->user,
12✔
1803
                           .pEffectiveUser = pRequest->effectiveUser,
12✔
1804
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
12✔
1805
                           .enableSysInfo = pTscObj->sysInfo,
12✔
1806
                           .async = true,
1807
                           .svrVer = pTscObj->sVer,
12✔
1808
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
12✔
1809
                           .allocatorId = pRequest->allocatorRefId,
12✔
1810
                           .parseSqlFp = clientParseSql,
1811
                           .parseSqlParam = pWrapper};
1812
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
12✔
1813
  (*pCxt)->biMode = biMode;
12✔
1814
  return TSDB_CODE_SUCCESS;
12✔
1815
}
1816

1817
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
12✔
1818
  STscStmt2*        pStmt = userdata;
12✔
1819
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
12✔
1820

1821
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
12✔
1822
  pStmt->affectedRows += pStmt->exec.affectedRows;
12✔
1823

1824
  fp(pStmt->options.userdata, res, code);
12✔
1825

1826
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
12!
1827
    taosUsleep(1);
×
1828
  }
1829
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
12✔
1830
  ++pStmt->sql.runTimes;
12✔
1831

1832
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
12!
1833
    tscError("failed to post asyncExecSem");
×
1834
  }
1835
}
12✔
1836

1837
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
276✔
1838
  STscStmt2* pStmt = (STscStmt2*)stmt;
276✔
1839
  int32_t    code = 0;
276✔
1840
  int64_t    startUs = taosGetTimestampUs();
276✔
1841

1842
  STMT_DLOG_E("start to exec");
276!
1843

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

1848
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
276!
1849
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
276!
1850
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1851
  }
1852
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
276!
1853

1854
  if (pStmt->sql.stbInterlaceMode) {
276✔
1855
    STMT_ERR_RET(stmtAddBatch2(pStmt));
229!
1856
  }
1857

1858
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
276✔
1859

1860
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
275✔
1861
    if (pStmt->sql.stbInterlaceMode) {
272✔
1862
      int64_t startTs = taosGetTimestampUs();
229✔
1863
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
906✔
1864
        taosUsleep(1);
677✔
1865
      }
1866
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
229✔
1867

1868
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
229!
1869
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
229✔
1870
      pStmt->sql.siInfo.pVgroupHash = NULL;
229✔
1871
      pStmt->sql.siInfo.pVgroupList = NULL;
229✔
1872
    } else {
1873
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
43✔
1874
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
43!
1875

1876
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
43!
1877

1878
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
43!
1879
    }
1880
  }
1881

1882
  SRequestObj*      pRequest = pStmt->exec.pRequest;
275✔
1883
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
275✔
1884

1885
  if (!fp) {
275✔
1886
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
263✔
1887

1888
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
263!
1889
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1890
      if (code) {
×
1891
        pStmt->exec.pRequest->code = code;
×
1892
      } else {
1893
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1894
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1895
      }
1896
    }
1897

1898
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
263!
1899

1900
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
263✔
1901
    if (affected_rows) {
263✔
1902
      *affected_rows = pStmt->exec.affectedRows;
260✔
1903
    }
1904
    pStmt->affectedRows += pStmt->exec.affectedRows;
263✔
1905

1906
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
263!
1907
      taosUsleep(1);
×
1908
    }
1909

1910
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
263!
1911

1912
    ++pStmt->sql.runTimes;
263✔
1913
  } else {
1914
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
12!
1915
    if (pWrapper == NULL) {
12!
1916
      code = terrno;
×
1917
    } else {
1918
      pWrapper->pRequest = pRequest;
12✔
1919
      pRequest->pWrapper = pWrapper;
12✔
1920
    }
1921
    if (TSDB_CODE_SUCCESS == code) {
12!
1922
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
12✔
1923
    }
1924
    pRequest->syncQuery = false;
12✔
1925
    pRequest->body.queryFp = asyncQueryCb;
12✔
1926
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
12✔
1927

1928
    pStmt->execSemWaited = false;
12✔
1929
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
12✔
1930
  }
1931

1932
_return:
275✔
1933
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
275✔
1934

1935
  STMT_RET(code);
275!
1936
}
1937

1938
int stmtClose2(TAOS_STMT2* stmt) {
88✔
1939
  STscStmt2* pStmt = (STscStmt2*)stmt;
88✔
1940

1941
  STMT_DLOG_E("start to free stmt");
88!
1942

1943
  if (pStmt->bindThreadInUse) {
88✔
1944
    pStmt->queue.stopQueue = true;
24✔
1945

1946
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
24✔
1947
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
24✔
1948
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
24✔
1949
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
24✔
1950

1951
    (void)taosThreadJoin(pStmt->bindThread, NULL);
24✔
1952
    pStmt->bindThreadInUse = false;
24✔
1953

1954
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
24✔
1955
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
24✔
1956
  }
1957

1958
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
88!
1959
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
88!
1960
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1961
  }
1962
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
88!
1963

1964
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
88✔
1965
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
88✔
1966

1967
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
88!
1968
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
5!
1969
      tscError("failed to wait asyncExecSem");
×
1970
    }
1971
  }
1972

1973
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
88!
1974
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1975
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1976
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1977
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1978
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1979
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1980
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1981
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1982
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1983

1984
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
88!
1985

1986
  if (pStmt->options.asyncExecFn) {
88✔
1987
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
5!
1988
      tscError("failed to destroy asyncExecSem");
×
1989
    }
1990
  }
1991
  taosMemoryFree(stmt);
88!
1992

1993
  return TSDB_CODE_SUCCESS;
88✔
1994
}
1995

1996
const char* stmtErrstr2(TAOS_STMT2* stmt) {
3✔
1997
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
1998

1999
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
3!
2000
    return (char*)tstrerror(terrno);
3✔
2001
  }
2002

2003
  pStmt->exec.pRequest->code = terrno;
×
2004

2005
  return taos_errstr(pStmt->exec.pRequest);
×
2006
}
2007
/*
2008
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2009

2010
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2011
*/
2012

2013
int stmtParseColFields2(TAOS_STMT2* stmt) {
48✔
2014
  int32_t    code = 0;
48✔
2015
  STscStmt2* pStmt = (STscStmt2*)stmt;
48✔
2016
  int32_t    preCode = pStmt->errCode;
48✔
2017

2018
  STMT_DLOG_E("start to get col fields");
48!
2019

2020
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
48!
2021
    return pStmt->errCode;
×
2022
  }
2023

2024
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
48!
2025
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2026
  }
2027

2028
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
48!
2029

2030
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
48!
2031
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
3!
2032
    pStmt->bInfo.needParse = false;
×
2033
  }
2034
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
48✔
2035
    pStmt->bInfo.needParse = false;
7✔
2036
  }
2037
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
48!
2038
    taos_free_result(pStmt->exec.pRequest);
×
2039
    pStmt->exec.pRequest = NULL;
×
2040
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2041
  }
2042

2043
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
48!
2044

2045
  if (pStmt->bInfo.needParse) {
48✔
2046
    STMT_ERRI_JRET(stmtParseSql(pStmt));
41✔
2047
  }
2048

2049
_return:
33✔
2050
  // compatible with previous versions
2051
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
48!
2052
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
2053
  }
2054

2055
  pStmt->errCode = preCode;
48✔
2056

2057
  return code;
48✔
2058
}
2059

2060
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
48✔
2061
  int32_t code = stmtParseColFields2(stmt);
48✔
2062
  if (code != TSDB_CODE_SUCCESS) {
48✔
2063
    return code;
15✔
2064
  }
2065

2066
  return stmtFetchStbColFields2(stmt, nums, fields);
33✔
2067
}
2068

2069
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
9✔
2070
  int32_t    code = 0;
9✔
2071
  STscStmt2* pStmt = (STscStmt2*)stmt;
9✔
2072
  int32_t    preCode = pStmt->errCode;
9✔
2073

2074
  STMT_DLOG_E("start to get param num");
9!
2075

2076
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9!
2077
    return pStmt->errCode;
×
2078
  }
2079

2080
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
9!
2081

2082
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9!
2083
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2084
    pStmt->bInfo.needParse = false;
×
2085
  }
2086

2087
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
9!
2088
    taos_free_result(pStmt->exec.pRequest);
×
2089
    pStmt->exec.pRequest = NULL;
×
2090
  }
2091

2092
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
9!
2093

2094
  if (pStmt->bInfo.needParse) {
9!
2095
    STMT_ERRI_JRET(stmtParseSql(pStmt));
9✔
2096
  }
2097

2098
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2!
2099
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
2✔
2100
  } else {
2101
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2102
  }
2103

2104
_return:
×
2105

2106
  pStmt->errCode = preCode;
9✔
2107

2108
  return code;
9✔
2109
}
2110

2111
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
3✔
2112
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
2113

2114
  STMT_DLOG_E("start to use result");
3!
2115

2116
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
3!
2117
    tscError("useResult only for query statement");
×
2118
    return NULL;
×
2119
  }
2120

2121
  return pStmt->exec.pRequest;
3✔
2122
}
2123

2124
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2125
  qInfo("async stmt bind thread started");
×
2126

2127
  ThreadArgs* targs = (ThreadArgs*)args;
×
2128
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2129

2130
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2131
  targs->fp(targs->param, NULL, code);
×
2132
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2133
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2134
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2135
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2136
  taosMemoryFree(args);
×
2137

2138
  qInfo("async stmt bind thread stopped");
×
2139

2140
  return code;
×
2141
}
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