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

taosdata / TDengine / #4873

04 Dec 2025 01:55AM UTC coverage: 64.558% (-0.1%) from 64.678%
#4873

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

719 of 2219 new or added lines in 36 files covered. (32.4%)

6363 existing lines in 135 files now uncovered.

159381 of 246882 relevant lines covered (64.56%)

108937395.15 hits per line

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

66.51
/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* gStmt2StatusStr[] = {"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) {
23,358,554✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
23,359,092✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
23,359,092✔
UNCOV
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;
23,359,092✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
23,362,702✔
42
  int i = 0;
23,362,702✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
94,990,982✔
44
    if (pStmt->queue.stopQueue) {
71,653,577✔
45
      return false;
21,626✔
46
    }
47
    if (i < 10) {
71,630,875✔
48
      taosUsleep(1);
68,160,035✔
49
      i++;
68,154,778✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
3,470,840✔
52
      if (pStmt->queue.stopQueue) {
3,473,502✔
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
3,473,502✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
3,467,806✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
3,473,413✔
60
    }
61
  }
62

63
  if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
23,324,010✔
64
    return false;
×
65
  }
66

67
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
23,324,279✔
68
  if (pStmt->queue.head == pStmt->queue.tail) {
23,340,513✔
69
    pStmt->queue.qRemainNum = 0;
×
70
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
71
    STMT2_ELOG_E("interlace queue is empty, cannot dequeue");
×
72
    return false;
×
73
  }
74

75
  SStmtQNode* node = pStmt->queue.head->next;
23,340,251✔
76
  pStmt->queue.head->next = node->next;
23,340,513✔
77
  if (pStmt->queue.tail == node) {
23,340,775✔
78
    pStmt->queue.tail = pStmt->queue.head;
11,364,186✔
79
  }
80
  node->next = NULL;
23,340,775✔
81
  *param = node;
23,340,513✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
23,340,775✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
23,342,450✔
85

86
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
23,339,946✔
87

88
  return true;
23,339,696✔
89
}
90

91
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
23,326,596✔
92
  if (param == NULL) {
23,326,596✔
93
    STMT2_ELOG_E("enqueue param is NULL");
×
94
    return;
×
95
  }
96

97
  param->next = NULL;
23,326,596✔
98

99
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
23,334,666✔
100

101
  pStmt->queue.tail->next = param;
23,340,818✔
102
  pStmt->queue.tail = param;
23,341,087✔
103
  pStmt->stat.bindDataNum++;
23,341,087✔
104

105
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
23,340,549✔
106
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
23,342,223✔
107

108
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
23,340,345✔
109

110
  STMT2_TLOG("enqueue param:%p, remainNum:%" PRId64 ", restoreTbCols:%d", param, pStmt->queue.qRemainNum,
23,339,348✔
111
             param->restoreTbCols);
112
}
113

114
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
27,969,315✔
115
  int32_t code = 0;
27,969,315✔
116

117
  if (pStmt->exec.pRequest == NULL) {
27,969,315✔
118
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
22,570✔
119
                        pStmt->reqid);
120
    if (pStmt->reqid != 0) {
22,299✔
121
      pStmt->reqid++;
242✔
122
    }
123
    pStmt->exec.pRequest->type = RES_TYPE__QUERY;
22,299✔
124
    if (pStmt->db != NULL) {
22,570✔
125
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
22,570✔
126
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
22,299✔
127
    }
128
    if (TSDB_CODE_SUCCESS == code) {
22,570✔
129
      pStmt->exec.pRequest->syncQuery = true;
22,570✔
130
      pStmt->exec.pRequest->stmtBindVersion = 2;
22,570✔
131
    }
132
    STMT2_DLOG("create request:0x%" PRIx64 ", QID:0x%" PRIx64, pStmt->exec.pRequest->self,
22,570✔
133
               pStmt->exec.pRequest->requestId);
134
  }
135

136
  return code;
27,970,987✔
137
}
138

139
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
58,564,188✔
140
  int32_t code = 0;
58,564,188✔
141

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
58,564,188✔
143
    STMT2_LOG_SEQ(newStatus);
58,572,996✔
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
58,581,462✔
147
    STMT2_ELOG("stmt already failed with err:%s, please use stmt prepare", tstrerror(pStmt->errCode));
×
148
    return pStmt->errCode;
×
149
  }
150

151
  switch (newStatus) {
58,581,193✔
152
    case STMT_PREPARE:
22,570✔
153
      pStmt->errCode = 0;
22,570✔
154
      break;
22,030✔
155
    case STMT_SETTBNAME:
16,035,368✔
156
      if (STMT_STATUS_EQ(INIT)) {
16,035,368✔
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
16,035,637✔
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
16,035,637✔
163
    case STMT_SETTAGS:
11,876,621✔
164
      if (STMT_STATUS_EQ(INIT)) {
11,876,621✔
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
11,876,621✔
168
    case STMT_FETCH_FIELDS:
691✔
169
      if (STMT_STATUS_EQ(INIT)) {
691✔
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
691✔
173
    case STMT_BIND:
16,033,508✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
16,033,508✔
175
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
176
      }
177
      /*
178
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
179
              code = TSDB_CODE_TSC_STMT_API_ERROR;
180
            }
181
      */
182
      break;
16,034,046✔
183
    case STMT_BIND_COL:
×
184
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
185
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
186
      }
187
      break;
×
188
    case STMT_ADD_BATCH:
7,306,594✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
7,306,594✔
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
7,306,863✔
193
    case STMT_EXECUTE:
7,305,841✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
7,305,841✔
195
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
242✔
196
            STMT_STATUS_NE(BIND_COL)) {
×
197
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
198
        }
199
      } else {
200
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
7,305,599✔
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
202
        }
203
      }
204
      break;
7,305,841✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
58,581,729✔
211

212
  pStmt->sql.status = newStatus;
58,581,729✔
213

214
  return TSDB_CODE_SUCCESS;
58,583,076✔
215
}
216

217
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
23,019✔
218
  STscStmt2* pStmt = (STscStmt2*)stmt;
23,019✔
219

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
23,019✔
221

222
  if ('\0' == pStmt->bInfo.tbName[0]) {
23,019✔
223
    tscWarn("no table name set, OK if it is a stmt get fields");
691✔
224
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
691✔
225
  }
226

227
  *tbName = pStmt->bInfo.tbName;
21,774✔
228

229
  return TSDB_CODE_SUCCESS;
22,328✔
230
}
231

232
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SArray* cols, SName* tbName,
23,019✔
233
                                  const char* sTableName, bool autoCreateTbl, int8_t tbNameFlag) {
234
  STscStmt2* pStmt = (STscStmt2*)stmt;
23,019✔
235
  char       tbFName[TSDB_TABLE_FNAME_LEN];
16,949✔
236
  int32_t    code = tNameExtractFullName(tbName, tbFName);
23,019✔
237
  if (code != 0) {
23,019✔
238
    return code;
×
239
  }
240

241
  if ((tags != NULL && ((SBoundColInfo*)tags)->numOfCols == 0) || !autoCreateTbl) {
23,019✔
242
    pStmt->sql.autoCreateTbl = false;
18,179✔
243
  }
244

245
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
23,019✔
246
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
23,019✔
247
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
23,019✔
248

249
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
23,019✔
250
  pStmt->bInfo.tbSuid = pTableMeta->suid;
23,019✔
251
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
23,019✔
252
  pStmt->bInfo.tbType = pTableMeta->tableType;
23,019✔
253

254
  if (!pStmt->bInfo.tagsCached) {
23,019✔
255
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
23,019✔
256
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
23,019✔
257
  }
258

259
  if (cols) {
23,019✔
260
    pStmt->bInfo.boundCols =
×
261
        tSimpleHashInit(taosArrayGetSize(cols), taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT));
×
262
    if (pStmt->bInfo.boundCols) {
×
263
      for (int32_t i = 0; i < taosArrayGetSize(cols); i++) {
×
264
        SColVal* pColVal = taosArrayGet(cols, i);
×
265
        if (pColVal) {
×
266
          code = tSimpleHashPut(pStmt->bInfo.boundCols, &pColVal->cid, sizeof(int16_t), pColVal, sizeof(SColVal));
×
267
          if (code != 0) {
×
268
            return code;
×
269
          }
270
        }
271
      }
272
    }
273
  } else {
274
    pStmt->bInfo.boundCols = NULL;
23,019✔
275
  }
276
  pStmt->bInfo.boundTags = tags;
23,019✔
277
  pStmt->bInfo.tagsCached = false;
23,019✔
278
  pStmt->bInfo.tbNameFlag = tbNameFlag;
23,019✔
279
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
23,019✔
280

281
  if (pTableMeta->tableType != TSDB_CHILD_TABLE && pTableMeta->tableType != TSDB_SUPER_TABLE) {
23,019✔
282
    pStmt->sql.stbInterlaceMode = false;
×
283
  }
284

285
  return TSDB_CODE_SUCCESS;
23,019✔
286
}
287

288
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
23,019✔
289
  STscStmt2* pStmt = (STscStmt2*)stmt;
23,019✔
290

291
  pStmt->sql.pVgHash = pVgHash;
23,019✔
292
  pStmt->exec.pBlockHash = pBlockHash;
23,019✔
293

294
  return TSDB_CODE_SUCCESS;
23,019✔
295
}
296

297
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SArray* cols, SName* tbName,
23,019✔
298
                              bool autoCreateTbl, SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName,
299
                              uint8_t tbNameFlag) {
300
  STscStmt2* pStmt = (STscStmt2*)stmt;
23,019✔
301

302
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
23,019✔
303
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
23,019✔
304

305
  pStmt->sql.autoCreateTbl = autoCreateTbl;
23,019✔
306

307
  return TSDB_CODE_SUCCESS;
23,019✔
308
}
309

310
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
691✔
311
  STscStmt2* pStmt = (STscStmt2*)stmt;
691✔
312

313
  *pVgHash = pStmt->sql.pVgHash;
691✔
314
  pStmt->sql.pVgHash = NULL;
691✔
315

316
  *pBlockHash = pStmt->exec.pBlockHash;
691✔
317
  pStmt->exec.pBlockHash = NULL;
691✔
318

319
  return TSDB_CODE_SUCCESS;
691✔
320
}
321

322
static int32_t stmtParseSql(STscStmt2* pStmt) {
22,707✔
323
  pStmt->exec.pCurrBlock = NULL;
22,707✔
324

325
  SStmtCallback stmtCb = {
23,261✔
326
      .pStmt = pStmt,
327
      .getTbNameFn = stmtGetTbName,
328
      .setInfoFn = stmtUpdateInfo,
329
      .getExecInfoFn = stmtGetExecInfo,
330
  };
331

332
  STMT_ERR_RET(stmtCreateRequest(pStmt));
23,261✔
333
  pStmt->exec.pRequest->stmtBindVersion = 2;
22,978✔
334

335
  pStmt->stat.parseSqlNum++;
23,261✔
336

337
  STMT2_DLOG("start to parse, QID:0x%" PRIx64, pStmt->exec.pRequest->requestId);
22,978✔
338
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
22,978✔
339

340
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
23,261✔
341

342
  pStmt->bInfo.needParse = false;
23,261✔
343

344
  if (pStmt->sql.type == 0) {
23,261✔
345
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
242✔
346
      pStmt->sql.type = STMT_TYPE_INSERT;
×
347
      pStmt->sql.stbInterlaceMode = false;
×
348
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
242✔
349
      pStmt->sql.type = STMT_TYPE_QUERY;
242✔
350
      pStmt->sql.stbInterlaceMode = false;
242✔
351

352
      return TSDB_CODE_SUCCESS;
242✔
353
    } else {
354
      STMT2_ELOG_E("only support select or insert sql");
×
355
      if (pStmt->exec.pRequest->msgBuf) {
×
356
        tstrncpy(pStmt->exec.pRequest->msgBuf, "stmt only support select or insert", pStmt->exec.pRequest->msgBufLen);
×
357
      }
358
      return TSDB_CODE_PAR_SYNTAX_ERROR;
×
359
    }
360
  } else if (pStmt->sql.type == STMT_TYPE_QUERY) {
23,019✔
361
    pStmt->sql.stbInterlaceMode = false;
×
362
    return TSDB_CODE_SUCCESS;
×
363
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
23,019✔
364
    pStmt->sql.stbInterlaceMode = false;
×
365
  }
366

367
  STableDataCxt** pSrc =
368
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
23,019✔
369
  if (NULL == pSrc || NULL == *pSrc) {
23,019✔
370
    STMT2_ELOG("fail to get exec.pBlockHash, maybe parse failed, tbFName:%s", pStmt->bInfo.tbFName);
×
371
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
372
  }
373

374
  STableDataCxt* pTableCtx = *pSrc;
23,019✔
375
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
23,019✔
376
    STMT2_TLOG("destroy pCreateTbReq for no-using insert, tbFName:%s", pStmt->bInfo.tbFName);
×
377
    tdDestroySVCreateTbReq(pTableCtx->pData->pCreateTbReq);
×
378
    taosMemoryFreeClear(pTableCtx->pData->pCreateTbReq);
×
379
    pTableCtx->pData->pCreateTbReq = NULL;
×
380
  }
381
  // if (pStmt->sql.stbInterlaceMode) {
382
  //   int16_t lastIdx = -1;
383

384
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
385
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
386
  //       pStmt->sql.stbInterlaceMode = false;
387
  //       break;
388
  //     }
389

390
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
391
  //   }
392
  // }
393

394
  if (NULL == pStmt->sql.pBindInfo) {
23,019✔
395
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
22,328✔
396
    if (NULL == pStmt->sql.pBindInfo) {
22,328✔
397
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
398
      return terrno;
×
399
    }
400
  }
401

402
  return TSDB_CODE_SUCCESS;
23,019✔
403
}
404

405
static void resetRequest(STscStmt2* pStmt) {
25,248✔
406
  if (pStmt->exec.pRequest) {
25,248✔
407
    taos_free_result(pStmt->exec.pRequest);
22,570✔
408
    pStmt->exec.pRequest = NULL;
22,570✔
409
  }
410
  pStmt->asyncResultAvailable = false;
25,248✔
411
}
25,248✔
412

413
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
7,348,486✔
414
  pStmt->bInfo.tbUid = 0;
7,348,486✔
415
  pStmt->bInfo.tbVgId = -1;
7,348,755✔
416
  pStmt->bInfo.tbType = 0;
7,348,755✔
417
  pStmt->bInfo.needParse = true;
7,348,755✔
418
  pStmt->bInfo.inExecCache = false;
7,348,755✔
419

420
  pStmt->bInfo.tbName[0] = 0;
7,348,755✔
421
  pStmt->bInfo.tbFName[0] = 0;
7,348,755✔
422
  if (!pStmt->bInfo.tagsCached) {
7,348,755✔
423
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
3,388,497✔
424
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
3,388,902✔
425
  }
426

427
  if (!pStmt->bInfo.boundColsCached) {
7,348,891✔
428
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
41,879✔
429
    pStmt->bInfo.boundCols = NULL;
41,879✔
430
  }
431

432
  if (!pStmt->sql.autoCreateTbl) {
7,348,353✔
433
    pStmt->bInfo.stbFName[0] = 0;
3,386,208✔
434
    pStmt->bInfo.tbSuid = 0;
3,386,208✔
435
  }
436

437
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
7,348,084✔
438
             pStmt->sql.autoCreateTbl);
439

440
  return TSDB_CODE_SUCCESS;
7,346,917✔
441
}
442

443
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
444
  (void)qResetStmtColumns(pTb->aCol, true);
×
445
  taosArrayDestroy(pTb->aCol);
×
446
}
×
447

448
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
7,298,295✔
449
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
7,298,295✔
450
  if (NULL == pTblBuf->pCurBuff) {
7,304,090✔
451
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
2,642✔
452
    return;
×
453
  }
454
  pTblBuf->buffIdx = 1;
7,301,448✔
455
  pTblBuf->buffOffset = sizeof(*pQueue->head);
7,301,448✔
456

457
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
7,301,448✔
458
  pQueue->qRemainNum = 0;
7,301,179✔
459
  pQueue->head->next = NULL;
7,301,179✔
460
}
461

462
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
7,322,704✔
463
  if (pStmt->sql.stbInterlaceMode) {
7,322,704✔
464
    if (deepClean) {
7,322,334✔
465
      taosHashCleanup(pStmt->exec.pBlockHash);
21,626✔
466
      pStmt->exec.pBlockHash = NULL;
21,626✔
467

468
      if (NULL != pStmt->exec.pCurrBlock) {
21,626✔
469
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
21,626✔
470
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
21,626✔
471
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
21,626✔
472
        pStmt->exec.pCurrBlock = NULL;
21,626✔
473
      }
474
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
21,626✔
475
        resetRequest(pStmt);
21,626✔
476
      }
477
    } else {
478
      pStmt->sql.siInfo.pTableColsIdx = 0;
7,300,708✔
479
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
7,300,977✔
480
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
7,301,605✔
481
    }
482
    if (NULL != pStmt->exec.pRequest) {
7,327,016✔
483
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
7,305,390✔
484
    }
485
  } else {
486
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
404✔
487
      resetRequest(pStmt);
8✔
488
    }
489

490
    size_t keyLen = 0;
1,888✔
491
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1,888✔
492
    while (pIter) {
7,916✔
493
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
6,028✔
494
      char*          key = taosHashGetKey(pIter, &keyLen);
6,028✔
495
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
6,028✔
496

497
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
6,028✔
498
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
702✔
499
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
1,646✔
500

501
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
702✔
502
        continue;
702✔
503
      }
504

505
      qDestroyStmtDataBlock(pBlocks);
5,326✔
506
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
5,326✔
507

508
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
5,326✔
509
    }
510

511
    if (keepTable) {
1,888✔
512
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
944✔
513
                 keepTable, deepClean);
514
      return TSDB_CODE_SUCCESS;
944✔
515
    }
516

517
    taosHashCleanup(pStmt->exec.pBlockHash);
944✔
518
    pStmt->exec.pBlockHash = NULL;
944✔
519

520
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
944✔
521
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
944✔
522
  }
523

524
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
7,327,960✔
525
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
7,325,475✔
526
             keepTable, deepClean);
527

528
  return TSDB_CODE_SUCCESS;
7,325,968✔
529
}
530

531
static void stmtFreeTbBuf(void* buf) {
21,626✔
532
  void* pBuf = *(void**)buf;
21,626✔
533
  taosMemoryFree(pBuf);
21,626✔
534
}
21,626✔
535

536
static void stmtFreeTbCols(void* buf) {
21,626,000✔
537
  SArray* pCols = *(SArray**)buf;
21,626,000✔
538
  taosArrayDestroy(pCols);
21,626,000✔
539
}
21,626,000✔
540

541
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
20,594✔
542
  STMT2_TLOG_E("start to free SQL info");
20,594✔
543

544
  taosMemoryFree(pStmt->sql.pBindInfo);
20,594✔
545
  taosMemoryFree(pStmt->sql.queryRes.fields);
20,594✔
546
  taosMemoryFree(pStmt->sql.queryRes.userFields);
20,594✔
547
  taosMemoryFree(pStmt->sql.sqlStr);
20,594✔
548
  qDestroyQuery(pStmt->sql.pQuery);
20,594✔
549
  taosArrayDestroy(pStmt->sql.nodeList);
20,594✔
550
  taosHashCleanup(pStmt->sql.pVgHash);
20,594✔
551
  pStmt->sql.pVgHash = NULL;
20,594✔
552
  if (pStmt->sql.fixValueTags) {
20,594✔
553
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
494✔
554
  }
555

556
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
20,594✔
557
  while (pIter) {
21,296✔
558
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
702✔
559

560
    qDestroyStmtDataBlock(pCache->pDataCtx);
702✔
561
    qDestroyBoundColInfo(pCache->boundTags);
702✔
562
    taosMemoryFreeClear(pCache->boundTags);
702✔
563

564
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
702✔
565
  }
566
  taosHashCleanup(pStmt->sql.pTableCache);
20,594✔
567
  pStmt->sql.pTableCache = NULL;
20,594✔
568

569
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
20,594✔
570
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
20,594✔
571

572
  taos_free_result(pStmt->sql.siInfo.pRequest);
20,594✔
573
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
20,594✔
574
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
20,594✔
575
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
20,594✔
576
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
20,594✔
577
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
20,594✔
578
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
20,594✔
579
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
20,594✔
580
  pStmt->sql.siInfo.pTableCols = NULL;
20,594✔
581

582
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
20,594✔
583
  pStmt->sql.siInfo.tableColsReady = true;
20,594✔
584

585
  STMT2_TLOG_E("end to free SQL info");
20,594✔
586

587
  return TSDB_CODE_SUCCESS;
20,594✔
588
}
589

590
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
11,871,369✔
591
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
11,871,369✔
592
    return TSDB_CODE_SUCCESS;
×
593
  }
594

595
  SVgroupInfo      vgInfo = {0};
11,871,369✔
596
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
11,871,369✔
597
                           .requestId = pStmt->exec.pRequest->requestId,
11,871,369✔
598
                           .requestObjRefId = pStmt->exec.pRequest->self,
11,871,369✔
599
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
11,871,369✔
600

601
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
11,878,486✔
602
  if (TSDB_CODE_SUCCESS != code) {
11,877,927✔
603
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
604
    return code;
×
605
  }
606

607
  code =
608
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
11,877,927✔
609
  if (TSDB_CODE_SUCCESS != code) {
11,876,631✔
610
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
×
611
    return code;
×
612
  }
613

614
  *vgId = vgInfo.vgId;
11,876,945✔
615

616
  return TSDB_CODE_SUCCESS;
11,876,945✔
617
}
618

619
int32_t stmtGetTableMetaAndValidate(STscStmt2* pStmt, uint64_t* uid, uint64_t* suid, int32_t* vgId, int8_t* tableType) {
18,179✔
620
  STableMeta*      pTableMeta = NULL;
18,179✔
621
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
18,179✔
622
                           .requestId = pStmt->exec.pRequest->requestId,
18,179✔
623
                           .requestObjRefId = pStmt->exec.pRequest->self,
18,179✔
624
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
18,179✔
625
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
18,179✔
626

627
  pStmt->stat.ctgGetTbMetaNum++;
18,179✔
628

629
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
18,179✔
630
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
×
631
    (void)stmtCleanBindInfo(pStmt);
×
632

633
    if (!pStmt->sql.autoCreateTbl) {
×
634
      STMT2_ELOG("table %s does not exist and autoCreateTbl is disabled", pStmt->bInfo.tbFName);
×
635
      STMT_ERR_RET(TSDB_CODE_PAR_TABLE_NOT_EXIST);
×
636
    }
637

638
    STMT_ERR_RET(code);
×
639
  }
640

641
  STMT_ERR_RET(code);
18,179✔
642

643
  *uid = pTableMeta->uid;
18,179✔
644
  *suid = pTableMeta->suid;
18,179✔
645
  *tableType = pTableMeta->tableType;
18,179✔
646
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
18,179✔
647
  *vgId = pTableMeta->vgId;
18,179✔
648

649
  taosMemoryFree(pTableMeta);
18,179✔
650

651
  return TSDB_CODE_SUCCESS;
18,179✔
652
}
653

654
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
4,624✔
655
                                    uint64_t suid, int32_t vgId) {
656
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
4,624✔
657
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
4,624✔
658

659
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
4,624✔
660

661
  return TSDB_CODE_SUCCESS;
4,624✔
662
}
663

664
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
26,952✔
665
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
26,952✔
666
    pStmt->bInfo.needParse = false;
×
667
    pStmt->bInfo.inExecCache = false;
×
668
    return TSDB_CODE_SUCCESS;
×
669
  }
670

671
  pStmt->bInfo.needParse = true;
26,952✔
672
  pStmt->bInfo.inExecCache = false;
26,952✔
673

674
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
26,952✔
675
  if (pCxtInExec) {
26,952✔
676
    pStmt->bInfo.needParse = false;
×
677
    pStmt->bInfo.inExecCache = true;
×
678

679
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
680

681
    if (pStmt->sql.autoCreateTbl) {
×
682
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
683
      return TSDB_CODE_SUCCESS;
×
684
    }
685
  }
686

687
  if (NULL == pStmt->pCatalog) {
26,952✔
688
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
20,352✔
689
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
20,081✔
690
  }
691

692
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
26,681✔
693
    if (pStmt->bInfo.inExecCache) {
22,057✔
694
      pStmt->bInfo.needParse = false;
×
695
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
696
      return TSDB_CODE_SUCCESS;
×
697
    }
698

699
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
22,328✔
700

701
    return TSDB_CODE_SUCCESS;
22,328✔
702
  }
703

704
  if (pStmt->sql.autoCreateTbl) {
4,624✔
705
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
4,624✔
706
    if (pCache) {
4,624✔
707
      pStmt->bInfo.needParse = false;
4,624✔
708
      pStmt->bInfo.tbUid = 0;
4,624✔
709

710
      STableDataCxt* pNewBlock = NULL;
4,624✔
711
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
4,624✔
712

713
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
4,624✔
714
                      POINTER_BYTES)) {
715
        STMT_ERR_RET(terrno);
×
716
      }
717

718
      pStmt->exec.pCurrBlock = pNewBlock;
4,624✔
719

720
      STMT2_DLOG("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
4,624✔
721

722
      return TSDB_CODE_SUCCESS;
4,624✔
723
    }
724

725
    STMT_RET(stmtCleanBindInfo(pStmt));
×
726
  }
727

728
  uint64_t uid, suid;
×
729
  int32_t  vgId;
×
730
  int8_t   tableType;
×
731

732
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
×
733

734
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
735

736
  if (uid == pStmt->bInfo.tbUid) {
×
737
    pStmt->bInfo.needParse = false;
×
738

739
    STMT2_DLOG("tb %s is current table", pStmt->bInfo.tbFName);
×
740

741
    return TSDB_CODE_SUCCESS;
×
742
  }
743

744
  if (pStmt->bInfo.inExecCache) {
×
745
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
746
    if (NULL == pCache) {
×
747
      STMT2_ELOG("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
748
                 pStmt->bInfo.tbFName, uid, cacheUid);
749

750
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
751
    }
752

753
    pStmt->bInfo.needParse = false;
×
754

755
    pStmt->bInfo.tbUid = uid;
×
756
    pStmt->bInfo.tbSuid = suid;
×
757
    pStmt->bInfo.tbType = tableType;
×
758
    pStmt->bInfo.boundTags = pCache->boundTags;
×
759
    pStmt->bInfo.tagsCached = true;
×
760

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

763
    return TSDB_CODE_SUCCESS;
×
764
  }
765

766
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
767
  if (pCache) {
×
768
    pStmt->bInfo.needParse = false;
×
769

770
    pStmt->bInfo.tbUid = uid;
×
771
    pStmt->bInfo.tbSuid = suid;
×
772
    pStmt->bInfo.tbType = tableType;
×
773
    pStmt->bInfo.boundTags = pCache->boundTags;
×
774
    pStmt->bInfo.tagsCached = true;
×
775

776
    STableDataCxt* pNewBlock = NULL;
×
777
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
778

779
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
780
                    POINTER_BYTES)) {
781
      STMT_ERR_RET(terrno);
×
782
    }
783

784
    pStmt->exec.pCurrBlock = pNewBlock;
×
785

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

788
    return TSDB_CODE_SUCCESS;
×
789
  }
790

791
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
792

793
  return TSDB_CODE_SUCCESS;
×
794
}
795

796
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
797
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
798

799
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
800
  if (NULL == pStmt->sql.pTableCache) {
×
801
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
802
    STMT_ERR_RET(terrno);
×
803
  }
804

805
  pStmt->sql.status = STMT_INIT;
×
806

807
  return TSDB_CODE_SUCCESS;
×
808
}
809

810
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
23,336,951✔
811
  SStmtQNode* pParam = (SStmtQNode*)param;
23,336,951✔
812

813
  if (pParam->restoreTbCols) {
23,336,951✔
814
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
23,338,030✔
815
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
16,034,160✔
816
      *p = taosArrayInit(20, POINTER_BYTES);
16,034,153✔
817
      if (*p == NULL) {
16,034,538✔
818
        pStmt->errCode = terrno;
1,583✔
819
      }
820
    }
821
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
7,303,870✔
822
    STMT2_TLOG_E("restore pTableCols finished");
7,305,209✔
823
  } else {
824
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
16,032,138✔
825
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
826
    // taosMemoryFree(pParam->pTbData);
827
    if (code != TSDB_CODE_SUCCESS) {
16,030,343✔
828
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
829
      pStmt->errCode = code;
×
830
    }
831
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
16,030,343✔
832
  }
833
}
23,342,317✔
834

835
static void* stmtBindThreadFunc(void* param) {
21,626✔
836
  setThreadName("stmt2Bind");
21,626✔
837

838
  STscStmt2* pStmt = (STscStmt2*)param;
21,626✔
839
  STMT2_ILOG_E("stmt2 bind thread started");
21,626✔
840

841
  while (true) {
23,341,669✔
842
    SStmtQNode* asyncParam = NULL;
23,363,295✔
843

844
    if (!stmtDequeue(pStmt, &asyncParam)) {
23,363,295✔
845
      if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
21,626✔
846
        STMT2_DLOG_E("queue is empty and stopQueue is set, thread will exit");
21,626✔
847
        break;
21,626✔
848
      }
849
      continue;
×
850
    }
851

852
    stmtAsyncOutput(pStmt, asyncParam);
23,337,998✔
853
  }
854

855
  STMT2_ILOG_E("stmt2 bind thread stopped");
21,895✔
856
  return NULL;
21,626✔
857
}
858

859
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
21,626✔
860
  TdThreadAttr thAttr;
16,949✔
861
  if (taosThreadAttrInit(&thAttr) != 0) {
21,626✔
862
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
863
  }
864
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
21,626✔
865
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
866
  }
867

868
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
21,626✔
869
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
870
    STMT_ERR_RET(terrno);
×
871
  }
872

873
  pStmt->bindThreadInUse = true;
21,626✔
874

875
  (void)taosThreadAttrDestroy(&thAttr);
21,626✔
876
  return TSDB_CODE_SUCCESS;
21,626✔
877
}
878

879
static int32_t stmtInitQueue(STscStmt2* pStmt) {
21,626✔
880
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
21,626✔
881
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
21,626✔
882
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
42,938✔
883
  pStmt->queue.tail = pStmt->queue.head;
21,469✔
884

885
  return TSDB_CODE_SUCCESS;
21,469✔
886
}
887

888
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
20,594✔
889
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
20,594✔
890
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
20,594✔
891
  pStmt->asyncBindParam.asyncBindNum = 0;
20,594✔
892

893
  return TSDB_CODE_SUCCESS;
20,594✔
894
}
895

896
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
21,626✔
897
  pTblBuf->buffUnit = sizeof(SStmtQNode);
21,626✔
898
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
21,626✔
899
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
21,626✔
900
  if (NULL == pTblBuf->pBufList) {
21,626✔
901
    return terrno;
×
902
  }
903
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
21,626✔
904
  if (NULL == buff) {
21,626✔
905
    return terrno;
×
906
  }
907

908
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
43,252✔
909
    return terrno;
×
910
  }
911

912
  pTblBuf->pCurBuff = buff;
21,626✔
913
  pTblBuf->buffIdx = 1;
21,626✔
914
  pTblBuf->buffOffset = 0;
21,626✔
915

916
  return TSDB_CODE_SUCCESS;
21,626✔
917
}
918

919
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
20,333✔
920
  STscObj*   pObj = (STscObj*)taos;
20,333✔
921
  STscStmt2* pStmt = NULL;
20,333✔
922
  int32_t    code = 0;
20,333✔
923

924
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
20,333✔
925
  if (NULL == pStmt) {
20,594✔
926
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
927
    return NULL;
×
928
  }
929

930
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
20,594✔
931
  if (NULL == pStmt->sql.pTableCache) {
20,594✔
932
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtInit2:%s", tstrerror(terrno));
×
933
    taosMemoryFree(pStmt);
×
934
    return NULL;
×
935
  }
936

937
  pStmt->taos = pObj;
20,594✔
938
  if (taos->db[0] != '\0') {
20,594✔
939
    pStmt->db = taosStrdup(taos->db);
20,594✔
940
  }
941
  pStmt->bInfo.needParse = true;
20,594✔
942
  pStmt->sql.status = STMT_INIT;
20,594✔
943
  pStmt->errCode = TSDB_CODE_SUCCESS;
20,594✔
944

945
  if (NULL != pOptions) {
20,594✔
946
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
19,903✔
947
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
19,903✔
948
      pStmt->stbInterlaceMode = true;
19,650✔
949
    }
950

951
    pStmt->reqid = pOptions->reqid;
19,903✔
952
  }
953

954
  if (pStmt->stbInterlaceMode) {
20,594✔
955
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
19,650✔
956
    pStmt->sql.siInfo.acctId = taos->acctId;
19,650✔
957
    pStmt->sql.siInfo.dbname = taos->db;
19,650✔
958
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
19,650✔
959

960
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
19,650✔
961
    if (NULL == pStmt->sql.siInfo.pTableHash) {
19,650✔
962
      STMT2_ELOG("fail to allocate memory for pTableHash:%s", tstrerror(terrno));
×
963
      (void)stmtClose2(pStmt);
×
964
      return NULL;
×
965
    }
966

967
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
19,650✔
968
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
19,650✔
969
      STMT2_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
970
      (void)stmtClose2(pStmt);
×
971
      return NULL;
×
972
    }
973

974
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
19,650✔
975
    if (NULL == pStmt->sql.siInfo.pTableCols) {
19,650✔
976
      STMT2_ELOG("fail to allocate memory for pTableCols:%s", tstrerror(terrno));
×
977
      (void)stmtClose2(pStmt);
×
978
      return NULL;
×
979
    }
980

981
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
19,650✔
982
    if (TSDB_CODE_SUCCESS == code) {
19,650✔
983
      code = stmtInitQueue(pStmt);
19,650✔
984
    }
985
    if (TSDB_CODE_SUCCESS == code) {
19,493✔
986
      code = stmtStartBindThread(pStmt);
19,493✔
987
    }
988
    if (TSDB_CODE_SUCCESS != code) {
19,650✔
989
      terrno = code;
×
990
      STMT2_ELOG("fail to init stmt2 bind thread:%s", tstrerror(code));
×
991
      (void)stmtClose2(pStmt);
×
992
      return NULL;
×
993
    }
994
  }
995

996
  pStmt->sql.siInfo.tableColsReady = true;
20,594✔
997
  if (pStmt->options.asyncExecFn) {
20,333✔
998
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
×
999
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1000
      STMT2_ELOG("fail to init asyncExecSem:%s", tstrerror(terrno));
×
1001
      (void)stmtClose2(pStmt);
×
1002
      return NULL;
×
1003
    }
1004
  }
1005
  code = stmtIniAsyncBind(pStmt);
20,594✔
1006
  if (TSDB_CODE_SUCCESS != code) {
20,594✔
1007
    terrno = code;
×
1008
    STMT2_ELOG("fail to start init asyncExecSem:%s", tstrerror(code));
×
1009

1010
    (void)stmtClose2(pStmt);
×
1011
    return NULL;
×
1012
  }
1013

1014
  pStmt->execSemWaited = false;
20,594✔
1015

1016
  // STMT_LOG_SEQ(STMT_INIT);
1017

1018
  STMT2_DLOG("stmt2 initialize finished, seqId:%d, db:%s, interlaceMode:%d, asyncExec:%d", pStmt->seqId, pStmt->db,
20,594✔
1019
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1020

1021
  return pStmt;
20,594✔
1022
}
1023

1024
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
1025
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1026
  if (dbName == NULL || dbName[0] == '\0') {
×
1027
    STMT2_ELOG_E("dbname in sql is illegal");
×
1028
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
1029
  }
1030

1031
  STMT2_DLOG("dbname is specified in sql:%s", dbName);
×
1032
  if (pStmt->db == NULL || pStmt->db[0] == '\0') {
×
1033
    taosMemoryFreeClear(pStmt->db);
×
1034
    STMT2_DLOG("dbname:%s is by sql, not by taosconnect", dbName);
×
1035
    pStmt->db = taosStrdup(dbName);
×
1036
    (void)strdequote(pStmt->db);
×
1037
  }
1038
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1039

1040
  // The SQL statement specifies a database name, overriding the previously specified database
1041
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
1042
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
×
1043
  (void)strdequote(pStmt->exec.pRequest->pDb);
×
1044
  if (pStmt->exec.pRequest->pDb == NULL) {
×
1045
    return terrno;
×
1046
  }
1047
  if (pStmt->sql.stbInterlaceMode) {
×
1048
    pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
×
1049
  }
1050
  return TSDB_CODE_SUCCESS;
×
1051
}
1052
static int32_t stmtResetStbInterlaceCache(STscStmt2* pStmt) {
1,976✔
1053
  int32_t code = TSDB_CODE_SUCCESS;
1,976✔
1054

1055
  if (pStmt->bindThreadInUse) {
1,976✔
1056
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
1,976✔
1057
      taosUsleep(1);
×
1058
    }
1059
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,976✔
1060
    pStmt->queue.stopQueue = true;
1,976✔
1061
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,976✔
1062
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,976✔
1063

1064
    (void)taosThreadJoin(pStmt->bindThread, NULL);
1,976✔
1065
    pStmt->bindThreadInUse = false;
1,976✔
1066
    pStmt->queue.head = NULL;
1,976✔
1067
    pStmt->queue.tail = NULL;
1,976✔
1068
    pStmt->queue.qRemainNum = 0;
1,976✔
1069

1070
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
1,976✔
1071
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
1,976✔
1072
  }
1073

1074
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
1,976✔
1075
  if (NULL == pStmt->sql.siInfo.pTableHash) {
1,976✔
1076
    return terrno;
×
1077
  }
1078

1079
  pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
1,976✔
1080
  if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
1,976✔
1081
    return terrno;
×
1082
  }
1083

1084
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
1,976✔
1085
  if (NULL == pStmt->sql.siInfo.pTableCols) {
1,976✔
1086
    return terrno;
×
1087
  }
1088

1089
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
1,976✔
1090

1091
  if (TSDB_CODE_SUCCESS == code) {
1,976✔
1092
    code = stmtInitQueue(pStmt);
1,976✔
1093
    pStmt->queue.stopQueue = false;
1,976✔
1094
  }
1095
  if (TSDB_CODE_SUCCESS == code) {
1,976✔
1096
    code = stmtStartBindThread(pStmt);
1,976✔
1097
  }
1098
  if (TSDB_CODE_SUCCESS != code) {
1,976✔
1099
    return code;
×
1100
  }
1101

1102
  return TSDB_CODE_SUCCESS;
1,976✔
1103
}
1104

1105
static int32_t stmtDeepReset(STscStmt2* pStmt) {
1,976✔
1106
  char*             db = pStmt->db;
1,976✔
1107
  bool              stbInterlaceMode = pStmt->stbInterlaceMode;
1,976✔
1108
  TAOS_STMT2_OPTION options = pStmt->options;
1,976✔
1109
  uint32_t          reqid = pStmt->reqid;
1,976✔
1110

1111
  pStmt->errCode = 0;
1,976✔
1112
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
1,976✔
1113
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1114
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1115
    }
1116
    pStmt->execSemWaited = true;
×
1117
  }
1118
  pStmt->sql.autoCreateTbl = false;
1,976✔
1119
  taosMemoryFree(pStmt->sql.pBindInfo);
1,976✔
1120
  pStmt->sql.pBindInfo = NULL;
1,976✔
1121

1122
  taosMemoryFree(pStmt->sql.queryRes.fields);
1,976✔
1123
  pStmt->sql.queryRes.fields = NULL;
1,976✔
1124

1125
  taosMemoryFree(pStmt->sql.queryRes.userFields);
1,976✔
1126
  pStmt->sql.queryRes.userFields = NULL;
1,976✔
1127

1128
  pStmt->sql.type = 0;
1,976✔
1129
  pStmt->sql.runTimes = 0;
1,976✔
1130
  taosMemoryFree(pStmt->sql.sqlStr);
1,976✔
1131
  pStmt->sql.sqlStr = NULL;
1,976✔
1132

1133
  qDestroyQuery(pStmt->sql.pQuery);
1,976✔
1134
  pStmt->sql.pQuery = NULL;
1,976✔
1135

1136
  taosArrayDestroy(pStmt->sql.nodeList);
1,976✔
1137
  pStmt->sql.nodeList = NULL;
1,976✔
1138

1139
  taosHashCleanup(pStmt->sql.pVgHash);
1,976✔
1140
  pStmt->sql.pVgHash = NULL;
1,976✔
1141

1142
  if (pStmt->sql.fixValueTags) {
1,976✔
1143
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
1,976✔
1144
    pStmt->sql.fixValueTbReq = NULL;
1,976✔
1145
  }
1146
  pStmt->sql.fixValueTags = false;
1,976✔
1147

1148
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
1,976✔
1149
  while (pIter) {
1,976✔
1150
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
1151

1152
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
1153
    qDestroyBoundColInfo(pCache->boundTags);
×
1154
    taosMemoryFreeClear(pCache->boundTags);
×
1155

1156
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
1157
  }
1158
  taosHashCleanup(pStmt->sql.pTableCache);
1,976✔
1159

1160
  if (pStmt->sql.stbInterlaceMode) {
1,976✔
1161
    pStmt->bInfo.tagsCached = false;
1,976✔
1162
  }
1163
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
1,976✔
1164

1165
  resetRequest(pStmt);
1,976✔
1166

1167
  if (pStmt->sql.siInfo.pTableCols) {
1,976✔
1168
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
1,976✔
1169
    pStmt->sql.siInfo.pTableCols = NULL;
1,976✔
1170
  }
1171

1172
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
1,976✔
1173
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
1,976✔
1174
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
1,976✔
1175
  }
1176

1177
  if (pStmt->sql.siInfo.pTableHash) {
1,976✔
1178
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
1,976✔
1179
    pStmt->sql.siInfo.pTableHash = NULL;
1,976✔
1180
  }
1181

1182
  if (pStmt->sql.siInfo.pTableRowDataHash) {
1,976✔
1183
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
1,976✔
1184
    pStmt->sql.siInfo.pTableRowDataHash = NULL;
1,976✔
1185
  }
1186

1187
  if (pStmt->sql.siInfo.pVgroupHash) {
1,976✔
1188
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1189
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1190
  }
1191

1192
  if (pStmt->sql.siInfo.pVgroupList) {
1,976✔
1193
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
1194
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1195
  }
1196

1197
  if (pStmt->sql.siInfo.pDataCtx) {
1,976✔
1198
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
1,976✔
1199
    pStmt->sql.siInfo.pDataCtx = NULL;
1,976✔
1200
  }
1201

1202
  if (pStmt->sql.siInfo.pTSchema) {
1,976✔
1203
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
1,976✔
1204
    pStmt->sql.siInfo.pTSchema = NULL;
1,976✔
1205
  }
1206

1207
  if (pStmt->sql.siInfo.pRequest) {
1,976✔
1208
    taos_free_result(pStmt->sql.siInfo.pRequest);
1,976✔
1209
    pStmt->sql.siInfo.pRequest = NULL;
1,976✔
1210
  }
1211

1212
  if (stbInterlaceMode) {
1,976✔
1213
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
1,976✔
1214
  }
1215

1216
  pStmt->db = db;
1,976✔
1217
  pStmt->stbInterlaceMode = stbInterlaceMode;
1,976✔
1218
  pStmt->options = options;
1,976✔
1219
  pStmt->reqid = reqid;
1,976✔
1220

1221
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1,976✔
1222
  if (NULL == pStmt->sql.pTableCache) {
1,976✔
1223
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
1224
    return terrno;
×
1225
  }
1226

1227
  pStmt->sql.status = STMT_INIT;
1,976✔
1228

1229
  return TSDB_CODE_SUCCESS;
1,976✔
1230
}
1231

1232
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
22,570✔
1233
  STscStmt2* pStmt = (STscStmt2*)stmt;
22,570✔
1234
  int32_t    code = 0;
22,570✔
1235

1236
  STMT2_DLOG("start to prepare with sql:%s", sql);
22,570✔
1237

1238
  if (stmt == NULL || sql == NULL) {
22,570✔
1239
    STMT2_ELOG_E("stmt or sql is NULL");
×
1240
    return TSDB_CODE_INVALID_PARA;
×
1241
  }
1242

1243
  if (pStmt->sql.status >= STMT_PREPARE) {
22,570✔
1244
    STMT2_DLOG("stmt status is %d, need to reset stmt2 cache before prepare", pStmt->sql.status);
1,976✔
1245
    STMT_ERR_RET(stmtDeepReset(pStmt));
1,976✔
1246
  }
1247

1248
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
22,570✔
1249
    STMT2_ELOG("errCode is not success before, ErrCode: 0x%x, errorsyt: %s\n. ", pStmt->errCode,
×
1250
               tstrerror(pStmt->errCode));
1251
    return pStmt->errCode;
×
1252
  }
1253

1254
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
22,570✔
1255

1256
  if (length <= 0) {
22,570✔
1257
    length = strlen(sql);
253✔
1258
  }
1259
  pStmt->sql.sqlStr = taosStrndup(sql, length);
22,570✔
1260
  if (!pStmt->sql.sqlStr) {
22,570✔
1261
    STMT2_ELOG("fail to allocate memory for sqlStr:%s", tstrerror(terrno));
×
1262
    STMT_ERR_RET(terrno);
×
1263
  }
1264
  pStmt->sql.sqlLen = length;
22,570✔
1265
  STMT_ERR_RET(stmtCreateRequest(pStmt));
22,570✔
1266

1267
  if (stmt2IsInsert(pStmt)) {
22,570✔
1268
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
22,328✔
1269
    char* dbName = NULL;
22,328✔
1270
    if (qParseDbName(sql, length, &dbName)) {
22,328✔
1271
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
1272
      taosMemoryFreeClear(dbName);
×
1273
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
22,057✔
1274
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
22,328✔
1275
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
22,599✔
1276
      if (pStmt->exec.pRequest->pDb == NULL) {
22,328✔
1277
        STMT_ERR_RET(terrno);
×
1278
      }
1279
      (void)strdequote(pStmt->exec.pRequest->pDb);
22,328✔
1280

1281
      if (pStmt->sql.stbInterlaceMode) {
22,057✔
1282
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
21,355✔
1283
      }
1284
    }
1285

1286
  } else if (stmt2IsSelect(pStmt)) {
242✔
1287
    pStmt->sql.stbInterlaceMode = false;
242✔
1288
    STMT_ERR_RET(stmtParseSql(pStmt));
242✔
1289
  } else {
1290
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
×
1291
  }
1292
  return TSDB_CODE_SUCCESS;
22,299✔
1293
}
1294

1295
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
21,626✔
1296
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
21,626✔
1297
  if (!pSrc) {
21,626✔
1298
    return terrno;
×
1299
  }
1300
  STableDataCxt* pDst = NULL;
21,626✔
1301

1302
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
21,626✔
1303
  pStmt->sql.siInfo.pDataCtx = pDst;
21,626✔
1304

1305
  SArray* pTblCols = NULL;
21,626✔
1306
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
21,616,136✔
1307
    pTblCols = taosArrayInit(20, POINTER_BYTES);
21,594,510✔
1308
    if (NULL == pTblCols) {
21,567,711✔
1309
      return terrno;
×
1310
    }
1311

1312
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
43,162,221✔
1313
      return terrno;
×
1314
    }
1315
  }
1316

1317
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
21,626✔
1318

1319
  STMT2_TLOG("init stb interlace table info, tbName:%s, pDataCtx:%p, boundTags:%p", pStmt->bInfo.tbFName,
21,626✔
1320
             pStmt->sql.siInfo.pDataCtx, pStmt->sql.siInfo.boundTags);
1321

1322
  return TSDB_CODE_SUCCESS;
21,626✔
1323
}
1324

1325
bool stmt2IsInsert(TAOS_STMT2* stmt) {
32,089,642✔
1326
  STscStmt2* pStmt = (STscStmt2*)stmt;
32,089,642✔
1327
  if (pStmt->sql.type) {
32,089,642✔
1328
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
32,051,277✔
1329
  }
1330

1331
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
39,174✔
1332
}
1333

1334
bool stmt2IsSelect(TAOS_STMT2* stmt) {
242✔
1335
  STscStmt2* pStmt = (STscStmt2*)stmt;
242✔
1336

1337
  if (pStmt->sql.type) {
242✔
1338
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1339
  }
1340
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
242✔
1341
}
1342

1343
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
16,034,701✔
1344
  STscStmt2* pStmt = (STscStmt2*)stmt;
16,034,701✔
1345

1346
  int64_t startUs = taosGetTimestampUs();
16,037,266✔
1347

1348
  STMT2_TLOG("start to set tbName:%s", tbName);
16,037,266✔
1349

1350
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
16,039,347✔
1351
    return pStmt->errCode;
×
1352
  }
1353

1354
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
16,040,152✔
1355

1356
  int32_t insert = 0;
16,037,124✔
1357
  if (!stmt2IsInsert(stmt)) {
16,037,124✔
1358
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
1359
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1360
  }
1361

1362
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
16,037,769✔
1363
    STMT_ERR_RET(stmtCreateRequest(pStmt));
31,333✔
1364

1365
    STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
26,952✔
1366
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1367
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
26,669✔
1368

1369
    STMT_ERR_RET(stmtGetFromCache(pStmt));
26,952✔
1370

1371
    if (pStmt->bInfo.needParse) {
26,952✔
1372
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
22,328✔
1373
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
22,328✔
1374

1375
      STMT_ERR_RET(stmtParseSql(pStmt));
22,328✔
1376
      if (!pStmt->sql.autoCreateTbl) {
22,328✔
1377
        uint64_t uid, suid;
16,949✔
1378
        int32_t  vgId;
16,949✔
1379
        int8_t   tableType;
16,949✔
1380

1381
        int32_t code = stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType);
18,179✔
1382
        if (code != TSDB_CODE_SUCCESS) {
18,179✔
1383
          return code;
×
1384
        }
1385
      }
1386
    }
1387

1388
  } else {
1389
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
16,006,436✔
1390
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
16,006,167✔
1391
    pStmt->exec.pRequest->requestId++;
16,005,898✔
1392
    pStmt->bInfo.needParse = false;
16,007,243✔
1393
  }
1394

1395
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
16,033,926✔
1396
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
21,626✔
1397
  }
1398

1399
  int64_t startUs2 = taosGetTimestampUs();
16,036,946✔
1400
  pStmt->stat.setTbNameUs += startUs2 - startUs;
16,036,946✔
1401

1402
  return TSDB_CODE_SUCCESS;
16,035,870✔
1403
}
1404

1405
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
9,901,619✔
1406
  STscStmt2* pStmt = (STscStmt2*)stmt;
9,901,619✔
1407

1408
  STMT2_TLOG_E("start to set tbTags");
9,901,619✔
1409

1410
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9,902,599✔
1411
    return pStmt->errCode;
×
1412
  }
1413

1414
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
9,902,599✔
1415

1416
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9,902,266✔
1417
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1418
    pStmt->bInfo.needParse = false;
×
1419
  }
1420
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,902,266✔
1421

1422
  if (pStmt->bInfo.needParse) {
9,901,374✔
1423
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1424
  }
1425
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
9,901,374✔
1426
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1427
  }
1428

1429
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
9,901,374✔
1430
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1431
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1432
  //   return TSDB_CODE_SUCCESS;
1433
  // }
1434
  if (pStmt->sql.autoCreateTbl && pStmt->sql.stbInterlaceMode) {
9,901,374✔
1435
    STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
9,895,508✔
1436
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1437
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
9,898,303✔
1438
  }
1439

1440
  STableDataCxt** pDataBlock = NULL;
9,903,943✔
1441
  if (pStmt->exec.pCurrBlock) {
9,903,943✔
1442
    pDataBlock = &pStmt->exec.pCurrBlock;
9,902,264✔
1443
  } else {
1444
    pDataBlock =
1445
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1,679✔
1446
    if (NULL == pDataBlock) {
1,679✔
1447
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1448
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1449
    }
1450
  }
1451
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
9,903,943✔
1452
    return TSDB_CODE_SUCCESS;
×
1453
  }
1454

1455
  STMT2_TLOG_E("start to bind stmt tag values");
9,903,943✔
1456

1457
  void* boundTags = NULL;
9,901,260✔
1458
  if (pStmt->sql.stbInterlaceMode) {
9,901,260✔
1459
    boundTags = pStmt->sql.siInfo.boundTags;
9,895,934✔
1460
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
9,895,934✔
1461
    if (NULL == pCreateTbReq) {
9,896,424✔
1462
      return terrno;
×
1463
    }
1464
    int32_t vgId = -1;
9,896,424✔
1465
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
9,896,424✔
1466
    (*pCreateTbReq)->uid = vgId;
9,899,597✔
1467
  } else {
1468
    boundTags = pStmt->bInfo.boundTags;
5,326✔
1469
  }
1470

1471
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
9,904,923✔
1472
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1473
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1474

1475
  return TSDB_CODE_SUCCESS;
9,903,522✔
1476
}
1477

1478
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
1,971,960✔
1479
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,971,960✔
1480

1481
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
1,971,960✔
1482

1483
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,974,271✔
1484
    return pStmt->errCode;
×
1485
  }
1486

1487
  if (!pStmt->sql.stbInterlaceMode) {
1,974,271✔
1488
    return TSDB_CODE_SUCCESS;
×
1489
  }
1490

1491
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
1,974,271✔
1492

1493
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,973,936✔
1494
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1495
    pStmt->bInfo.needParse = false;
×
1496
  }
1497
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,973,936✔
1498

1499
  if (pStmt->bInfo.needParse) {
1,972,117✔
1500
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1501
    if (!pStmt->sql.autoCreateTbl) {
×
1502
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1503
      return TSDB_CODE_SUCCESS;
×
1504
    }
1505
  }
1506

1507
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,972,117✔
1508
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1509
  }
1510

1511
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,972,117✔
1512
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1513
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,973,823✔
1514

1515
  STableDataCxt** pDataBlock = NULL;
1,972,948✔
1516
  if (pStmt->exec.pCurrBlock) {
1,972,948✔
1517
    pDataBlock = &pStmt->exec.pCurrBlock;
1,970,478✔
1518
  } else {
1519
    pDataBlock =
1520
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,470✔
1521
    if (NULL == pDataBlock) {
2,470✔
1522
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1523
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
2,738✔
1524
    }
1525
  }
1526

1527
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
1,975,686✔
1528
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1529
    return TSDB_CODE_SUCCESS;
×
1530
  }
1531

1532
  if (pStmt->sql.fixValueTags) {
1,975,686✔
1533
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
1,973,216✔
1534
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
1,973,216✔
1535
    if ((*pCreateTbReq)->name) {
1,970,321✔
1536
      taosMemoryFree((*pCreateTbReq)->name);
1,971,353✔
1537
    }
1538
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
1,971,713✔
1539
    int32_t vgId = -1;
1,973,126✔
1540
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
1,973,126✔
1541
    (*pCreateTbReq)->uid = vgId;
1,972,969✔
1542
    return TSDB_CODE_SUCCESS;
1,972,969✔
1543
  }
1544

1545
  if ((*pDataBlock)->pData->pCreateTbReq) {
2,470✔
1546
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
2,470✔
1547
    pStmt->sql.fixValueTags = true;
2,470✔
1548
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
2,470✔
1549
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
2,470✔
1550
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
2,470✔
1551
  }
1552

1553
  return TSDB_CODE_SUCCESS;
2,470✔
1554
}
1555

1556
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1557
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1558
    return pStmt->errCode;
×
1559
  }
1560

1561
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1562
    tscError("invalid operation to get query column fileds");
×
1563
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1564
  }
1565

1566
  STableDataCxt** pDataBlock = NULL;
×
1567

1568
  if (pStmt->sql.stbInterlaceMode) {
×
1569
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1570
  } else {
1571
    pDataBlock =
1572
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1573
    if (NULL == pDataBlock) {
×
1574
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1575
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1576
    }
1577
  }
1578

1579
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1580

1581
  return TSDB_CODE_SUCCESS;
×
1582
}
1583

1584
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
691✔
1585
  int32_t code = 0;
691✔
1586
  int32_t preCode = pStmt->errCode;
691✔
1587

1588
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
691✔
1589
    return pStmt->errCode;
×
1590
  }
1591

1592
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
691✔
1593
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1594
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1595
  }
1596

1597
  STableDataCxt** pDataBlock = NULL;
691✔
1598
  bool            cleanStb = false;
691✔
1599

1600
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
691✔
1601
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1602
  } else {
1603
    cleanStb = true;
691✔
1604
    pDataBlock =
1605
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
691✔
1606
  }
1607

1608
  if (NULL == pDataBlock || NULL == *pDataBlock) {
691✔
1609
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1610
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1611
  }
1612

1613
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.boundCols,
691✔
1614
                                        pStmt->bInfo.tbNameFlag, fieldNum, fields));
1615

1616
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
691✔
1617
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
691✔
1618
    qDestroyStmtDataBlock(*pDataBlock);
691✔
1619
    *pDataBlock = NULL;
691✔
1620
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
691✔
1621
      STMT2_ELOG("fail to remove remove stb:%s exec blockHash", pStmt->bInfo.tbFName);
×
1622
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1623
    }
1624
    pStmt->sql.autoCreateTbl = false;
691✔
1625
    pStmt->bInfo.tagsCached = false;
691✔
1626
    pStmt->bInfo.sname = (SName){0};
691✔
1627
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
691✔
1628
  }
1629

1630
_return:
×
1631

1632
  pStmt->errCode = preCode;
691✔
1633

1634
  return code;
691✔
1635
}
1636
/*
1637
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1638
  while (true) {
1639
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1640
      pStmt->exec.smInfo.pColIdx = 0;
1641
    }
1642

1643
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1644
      taosUsleep(1);
1645
      continue;
1646
    }
1647

1648
    *idx = pStmt->exec.smInfo.pColIdx;
1649
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1650
  }
1651
}
1652
*/
1653
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
16,031,745✔
1654
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
16,031,745✔
1655
    pStmt->sql.siInfo.pVgroupHash =
7,304,470✔
1656
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
7,303,172✔
1657
  }
1658
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
16,033,850✔
1659
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
7,304,133✔
1660
  }
1661

1662
  if (NULL == pStmt->sql.siInfo.pRequest) {
16,033,199✔
1663
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
21,626✔
1664
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1665

1666
    if (pStmt->reqid != 0) {
21,626✔
1667
      pStmt->reqid++;
242✔
1668
    }
1669
    pStmt->exec.pRequest->syncQuery = true;
21,626✔
1670

1671
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
21,626✔
1672
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
21,626✔
1673
  }
1674

1675
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
16,032,930✔
1676
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
127,040✔
1677
    pStmt->sql.siInfo.tbFromHash = true;
12,672✔
1678
  }
1679

1680
  if (0 == pStmt->sql.siInfo.firstName[0]) {
16,032,392✔
1681
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
19,650✔
1682
  }
1683

1684
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
16,032,661✔
1685
  param->next = NULL;
16,032,661✔
1686

1687
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
16,032,392✔
1688

1689
  if (pStmt->queue.stopQueue) {
16,037,577✔
1690
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1691
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1692
  }
1693
  stmtEnqueue(pStmt, param);
16,036,770✔
1694

1695
  return TSDB_CODE_SUCCESS;
16,035,071✔
1696
}
1697

1698
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1699
  while (true) {
1700
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
16,033,780✔
1701
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
16,033,776✔
1702
      break;
16,033,776✔
1703
    } else {
1704
      SArray* pTblCols = NULL;
×
1705
      for (int32_t i = 0; i < 100; i++) {
×
1706
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1707
        if (NULL == pTblCols) {
×
1708
          return terrno;
×
1709
        }
1710

1711
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1712
          return terrno;
×
1713
        }
1714
      }
1715
    }
1716
  }
1717

1718
  return TSDB_CODE_SUCCESS;
16,033,776✔
1719
}
1720

1721
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
5,326✔
1722
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
5,326✔
1723
    return TSDB_CODE_SUCCESS;
×
1724
  }
1725

1726
  uint64_t uid = pStmt->bInfo.tbUid;
5,326✔
1727
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
5,326✔
1728

1729
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
5,326✔
1730
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
4,624✔
1731
    return TSDB_CODE_SUCCESS;
4,624✔
1732
  }
1733

1734
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
702✔
1735
  if (!pSrc) {
702✔
1736
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1737
    return terrno;
×
1738
  }
1739
  STableDataCxt* pDst = NULL;
702✔
1740

1741
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
702✔
1742

1743
  SStmtTableCache cache = {
702✔
1744
      .pDataCtx = pDst,
1745
      .boundTags = pStmt->bInfo.boundTags,
702✔
1746
  };
1747

1748
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
702✔
1749
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
1750
    return terrno;
×
1751
  }
1752

1753
  if (pStmt->sql.autoCreateTbl) {
702✔
1754
    pStmt->bInfo.tagsCached = true;
702✔
1755
  } else {
1756
    pStmt->bInfo.boundTags = NULL;
×
1757
  }
1758

1759
  return TSDB_CODE_SUCCESS;
702✔
1760
}
1761

1762
static int stmtAddBatch2(TAOS_STMT2* stmt) {
7,308,902✔
1763
  STscStmt2* pStmt = (STscStmt2*)stmt;
7,308,902✔
1764

1765
  int64_t startUs = taosGetTimestampUs();
7,309,888✔
1766

1767
  // STMT2_TLOG_E("start to add batch");
1768

1769
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,309,888✔
1770
    return pStmt->errCode;
×
1771
  }
1772

1773
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
7,309,888✔
1774

1775
  if (pStmt->sql.stbInterlaceMode) {
7,308,162✔
1776
    int64_t startUs2 = taosGetTimestampUs();
7,304,740✔
1777
    pStmt->stat.addBatchUs += startUs2 - startUs;
7,304,740✔
1778

1779
    pStmt->sql.siInfo.tableColsReady = false;
7,304,740✔
1780

1781
    SStmtQNode* param = NULL;
7,304,740✔
1782
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
14,609,390✔
1783
    param->restoreTbCols = true;
7,304,650✔
1784
    param->next = NULL;
7,304,650✔
1785

1786
    if (pStmt->sql.autoCreateTbl) {
7,304,650✔
1787
      pStmt->bInfo.tagsCached = true;
3,955,531✔
1788
    }
1789
    pStmt->bInfo.boundColsCached = true;
7,304,381✔
1790

1791
    if (pStmt->queue.stopQueue) {
7,304,381✔
1792
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1793
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1794
    }
1795

1796
    stmtEnqueue(pStmt, param);
7,304,650✔
1797

1798
    return TSDB_CODE_SUCCESS;
7,305,231✔
1799
  }
1800

1801
  STMT_ERR_RET(stmtCacheBlock(pStmt));
5,326✔
1802

1803
  return TSDB_CODE_SUCCESS;
5,326✔
1804
}
1805
/*
1806
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1807
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1808
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1809
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1810

1811
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1812
  pRes->fields = taosMemoryMalloc(size);
1813
  pRes->userFields = taosMemoryMalloc(size);
1814
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1815
    STMT_ERR_RET(terrno);
1816
  }
1817
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1818
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1819

1820
  return TSDB_CODE_SUCCESS;
1821
}
1822

1823
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1824
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1825
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1826

1827
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1828
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1829

1830
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1831
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1832
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1833
      STMT_ERR_RET(terrno);
1834
    }
1835
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1836
  }
1837

1838
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1839
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1840
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1841
      STMT_ERR_RET(terrno);
1842
    }
1843
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1844
  }
1845

1846
  return TSDB_CODE_SUCCESS;
1847
}
1848
*/
1849

1850
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
16,032,272✔
1851
  STscStmt2* pStmt = (STscStmt2*)stmt;
16,032,272✔
1852
  int32_t    code = 0;
16,032,272✔
1853

1854
  int64_t startUs = taosGetTimestampUs();
16,036,950✔
1855

1856
  STMT2_TLOG("start to bind data, colIdx:%d", colIdx);
16,036,950✔
1857

1858
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
16,034,602✔
1859
    return pStmt->errCode;
×
1860
  }
1861

1862
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
16,035,409✔
1863

1864
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
16,034,458✔
1865
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1866
    pStmt->bInfo.needParse = false;
×
1867
  }
1868

1869
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
16,033,382✔
1870
    resetRequest(pStmt);
×
1871
  }
1872

1873
  STMT_ERR_RET(stmtCreateRequest(pStmt));
16,033,651✔
1874
  if (pStmt->bInfo.needParse) {
16,033,809✔
1875
    code = stmtParseSql(pStmt);
×
1876
    if (code != TSDB_CODE_SUCCESS) {
×
1877
      goto cleanup_root;
×
1878
    }
1879
  }
1880

1881
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
16,034,078✔
1882
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
242✔
1883
    if (code != TSDB_CODE_SUCCESS) {
242✔
1884
      goto cleanup_root;
×
1885
    }
1886
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
242✔
1887
                         .acctId = pStmt->taos->acctId,
242✔
1888
                         .db = pStmt->exec.pRequest->pDb,
242✔
1889
                         .topicQuery = false,
1890
                         .pSql = pStmt->sql.sqlStr,
242✔
1891
                         .sqlLen = pStmt->sql.sqlLen,
242✔
1892
                         .pMsg = pStmt->exec.pRequest->msgBuf,
242✔
1893
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1894
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
242✔
1895
                         .pStmtCb = NULL,
1896
                         .pUser = pStmt->taos->user,
242✔
1897
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
242✔
1898
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
242✔
1899
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
242✔
1900
    if (code != TSDB_CODE_SUCCESS) {
242✔
1901
      goto cleanup_root;
×
1902
    }
1903
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
242✔
1904
    if (code != TSDB_CODE_SUCCESS) {
242✔
1905
      goto cleanup_root;
×
1906
    }
1907

1908
    if (pStmt->sql.pQuery->haveResultSet) {
242✔
1909
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
484✔
1910
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1911
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
242✔
1912
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
242✔
1913
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
242✔
1914
    }
1915

1916
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
242✔
1917
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
242✔
1918
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
242✔
1919

1920
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1921
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1922
    // }
1923

1924
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1925

1926
    return TSDB_CODE_SUCCESS;
242✔
1927

1928
  cleanup_root:
×
1929
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
×
1930
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
×
1931
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
×
1932
      pStmt->sql.pQuery->pRoot = NULL;
×
1933
    }
1934
    STMT_ERR_RET(code);
×
1935
  }
1936

1937
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
16,033,836✔
1938
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1939
  }
1940

1941
  STableDataCxt** pDataBlock = NULL;
16,032,528✔
1942

1943
  if (pStmt->exec.pCurrBlock) {
16,032,528✔
1944
    pDataBlock = &pStmt->exec.pCurrBlock;
16,011,545✔
1945
  } else {
1946
    pDataBlock =
1947
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
22,328✔
1948
    if (NULL == pDataBlock) {
22,328✔
1949
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1950
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1951
    }
1952
    pStmt->exec.pCurrBlock = *pDataBlock;
22,328✔
1953
    if (pStmt->sql.stbInterlaceMode) {
22,328✔
1954
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
21,626✔
1955
      (*pDataBlock)->pData->aCol = NULL;
21,626✔
1956
    }
1957
    if (colIdx < -1) {
22,328✔
1958
      pStmt->sql.bindRowFormat = true;
×
1959
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
1960
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
1961
    }
1962
  }
1963

1964
  int64_t startUs2 = taosGetTimestampUs();
16,034,000✔
1965
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
16,034,000✔
1966

1967
  SStmtQNode* param = NULL;
16,033,193✔
1968
  if (pStmt->sql.stbInterlaceMode) {
16,033,193✔
1969
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
32,064,780✔
1970
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
32,066,749✔
1971
    taosArrayClear(param->tblData.aCol);
16,033,776✔
1972

1973
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1974

1975
    param->restoreTbCols = false;
16,021,913✔
1976
    param->tblData.isOrdered = true;
16,021,913✔
1977
    param->tblData.isDuplicateTs = false;
16,021,913✔
1978
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
16,021,913✔
1979

1980
    param->pCreateTbReq = pCreateTbReq;
16,022,720✔
1981
  }
1982

1983
  int64_t startUs3 = taosGetTimestampUs();
16,030,345✔
1984
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
16,030,345✔
1985

1986
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
16,032,228✔
1987
  SBlobSet* pBlob = NULL;
16,033,304✔
1988
  if (colIdx < 0) {
16,032,766✔
1989
    if (pStmt->sql.stbInterlaceMode) {
16,034,310✔
1990
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
16,029,166✔
1991
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
18,008,232✔
1992
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
16,029,704✔
1993
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
16,029,704✔
1994
      param->tblData.isOrdered = (*pDataBlock)->ordered;
16,033,262✔
1995
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
16,034,069✔
1996
    } else {
1997
      if (colIdx == -1) {
5,682✔
1998
        if (pStmt->sql.bindRowFormat) {
5,326✔
1999
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2000
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2001
        }
2002
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
5,326✔
2003
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
5,326✔
2004
      } else {
2005
        code =
2006
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
356✔
2007
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
356✔
2008
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
356✔
2009
      }
2010
    }
2011

2012
    if (code) {
16,038,319✔
2013
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
×
2014
      STMT_ERR_RET(code);
×
2015
    }
2016
  } else {
2017
    if (pStmt->sql.stbInterlaceMode) {
538✔
2018
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2019
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2020
    }
2021

2022
    if (pStmt->sql.bindRowFormat) {
×
2023
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2024
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2025
    }
2026

2027
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
2028
      STMT2_ELOG_E("bind column index not in sequence");
×
2029
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2030
    }
2031

2032
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2033

2034
    if (0 == colIdx) {
×
2035
      pStmt->bInfo.sBindRowNum = bind->num;
×
2036
    }
2037

2038
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
2039
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
×
2040
                                    pStmt->taos->optionInfo.charsetCxt);
×
2041
    if (code) {
×
2042
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
2043
      STMT_ERR_RET(code);
×
2044
    }
2045
  }
2046

2047
  int64_t startUs4 = taosGetTimestampUs();
16,037,834✔
2048
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
16,037,834✔
2049

2050
  if (pStmt->stbInterlaceMode) {
16,038,103✔
2051
    if (param) param->tblData.pBlobSet = pBlob;
16,034,746✔
2052
  }
2053

2054
  if (pStmt->sql.stbInterlaceMode) {
16,038,910✔
2055
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
16,034,653✔
2056
  } else {
2057
    STMT_ERR_RET(stmtAddBatch2(pStmt));
3,996✔
2058
  }
2059

2060
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
16,039,922✔
2061
  return TSDB_CODE_SUCCESS;
16,039,653✔
2062
}
2063

2064
/*
2065
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2066
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2067

2068
  int32_t code = 0;
2069
  int32_t finalCode = 0;
2070
  size_t  keyLen = 0;
2071
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2072
  while (pIter) {
2073
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2074
    char*          key = taosHashGetKey(pIter, &keyLen);
2075

2076
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2077
    if (pMeta->uid) {
2078
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2079
      continue;
2080
    }
2081

2082
    SSubmitBlkRsp* blkRsp = NULL;
2083
    int32_t        i = 0;
2084
    for (; i < pRsp->nBlocks; ++i) {
2085
      blkRsp = pRsp->pBlocks + i;
2086
      if (strlen(blkRsp->tblFName) != keyLen) {
2087
        continue;
2088
      }
2089

2090
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2091
        continue;
2092
      }
2093

2094
      break;
2095
    }
2096

2097
    if (i < pRsp->nBlocks) {
2098
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2099
               blkRsp->uid);
2100

2101
      pMeta->uid = blkRsp->uid;
2102
      pStmt->bInfo.tbUid = blkRsp->uid;
2103
    } else {
2104
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2105
      if (NULL == pStmt->pCatalog) {
2106
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2107
        if (code) {
2108
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2109
          finalCode = code;
2110
          continue;
2111
        }
2112
      }
2113

2114
      code = stmtCreateRequest(pStmt);
2115
      if (code) {
2116
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2117
        finalCode = code;
2118
        continue;
2119
      }
2120

2121
      STableMeta*      pTableMeta = NULL;
2122
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2123
                               .requestId = pStmt->exec.pRequest->requestId,
2124
                               .requestObjRefId = pStmt->exec.pRequest->self,
2125
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2126
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2127

2128
      pStmt->stat.ctgGetTbMetaNum++;
2129

2130
      taos_free_result(pStmt->exec.pRequest);
2131
      pStmt->exec.pRequest = NULL;
2132

2133
      if (code || NULL == pTableMeta) {
2134
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2135
        finalCode = code;
2136
        taosMemoryFree(pTableMeta);
2137
        continue;
2138
      }
2139

2140
      pMeta->uid = pTableMeta->uid;
2141
      pStmt->bInfo.tbUid = pTableMeta->uid;
2142
      taosMemoryFree(pTableMeta);
2143
    }
2144

2145
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2146
  }
2147

2148
  return finalCode;
2149
}
2150
*/
2151
/*
2152
int stmtStaticModeExec(TAOS_STMT* stmt) {
2153
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2154
  int32_t     code = 0;
2155
  SSubmitRsp* pRsp = NULL;
2156
  if (pStmt->sql.staticMode) {
2157
    return TSDB_CODE_TSC_STMT_API_ERROR;
2158
  }
2159

2160
  STMT_DLOG_E("start to exec");
2161

2162
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2163

2164
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2165
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2166

2167
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2168

2169
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2170
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2171
    if (code) {
2172
      pStmt->exec.pRequest->code = code;
2173
    } else {
2174
      tFreeSSubmitRsp(pRsp);
2175
      STMT_ERR_RET(stmtResetStmt(pStmt));
2176
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2177
    }
2178
  }
2179

2180
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2181

2182
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2183
  pStmt->affectedRows += pStmt->exec.affectedRows;
2184

2185
_return:
2186

2187
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2188

2189
  tFreeSSubmitRsp(pRsp);
2190

2191
  ++pStmt->sql.runTimes;
2192

2193
  STMT_RET(code);
2194
}
2195
*/
2196

2197
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
2198
  const STscObj* pTscObj = pRequest->pTscObj;
×
2199

2200
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
2201
  if (*pCxt == NULL) {
×
2202
    return terrno;
×
2203
  }
2204

2205
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
2206
                           .requestRid = pRequest->self,
×
2207
                           .acctId = pTscObj->acctId,
×
2208
                           .db = pRequest->pDb,
×
2209
                           .topicQuery = false,
2210
                           .pSql = pRequest->sqlstr,
×
2211
                           .sqlLen = pRequest->sqlLen,
×
2212
                           .pMsg = pRequest->msgBuf,
×
2213
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2214
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
2215
                           .pStmtCb = NULL,
2216
                           .pUser = pTscObj->user,
×
2217
                           .pEffectiveUser = pRequest->effectiveUser,
×
2218
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
2219
                           .enableSysInfo = pTscObj->sysInfo,
×
2220
                           .async = true,
2221
                           .svrVer = pTscObj->sVer,
×
2222
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
2223
                           .allocatorId = pRequest->allocatorRefId,
×
2224
                           .parseSqlFp = clientParseSql,
2225
                           .parseSqlParam = pWrapper};
2226
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
2227
  (*pCxt)->biMode = biMode;
×
2228
  return TSDB_CODE_SUCCESS;
×
2229
}
2230

2231
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
2232
  STscStmt2*        pStmt = userdata;
×
2233
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
2234
  pStmt->asyncResultAvailable = true;
×
2235
  pStmt->exec.pRequest->inCallback = true;
×
2236

2237
  if (code == TSDB_CODE_SUCCESS) {
×
2238
    pStmt->exec.affectedRows = taos_affected_rows(res);
×
2239
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
2240
  }
2241

2242
  fp(pStmt->options.userdata, res, code);
×
2243

2244
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
2245
    taosUsleep(1);
×
2246
  }
2247
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
2248
  ++pStmt->sql.runTimes;
×
2249
  if (pStmt->exec.pRequest != NULL) {
×
2250
    pStmt->exec.pRequest->inCallback = false;
×
2251
  }
2252

2253
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
2254
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2255
  }
2256
}
×
2257

2258
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
7,302,307✔
2259
  STscStmt2* pStmt = (STscStmt2*)stmt;
7,302,307✔
2260
  int32_t    code = 0;
7,302,307✔
2261
  int64_t    startUs = taosGetTimestampUs();
7,305,465✔
2262

2263
  STMT2_DLOG_E("start to exec");
7,305,465✔
2264

2265
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,305,731✔
2266
    return pStmt->errCode;
×
2267
  }
2268

2269
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
7,305,731✔
2270
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
7,306,422✔
2271
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2272
  }
2273
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
7,304,294✔
2274

2275
  if (pStmt->sql.stbInterlaceMode) {
7,306,177✔
2276
    STMT_ERR_RET(stmtAddBatch2(pStmt));
7,305,076✔
2277
  }
2278

2279
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
7,306,668✔
2280

2281
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
7,304,787✔
2282
    if (pStmt->sql.stbInterlaceMode) {
7,305,173✔
2283
      int64_t startTs = taosGetTimestampUs();
7,305,322✔
2284
      // wait for stmt bind thread to finish
2285
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
25,979,559✔
2286
        taosUsleep(1);
18,670,456✔
2287
      }
2288

2289
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,305,231✔
2290
        return pStmt->errCode;
×
2291
      }
2292

2293
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
7,304,965✔
2294
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
7,304,965✔
2295
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
7,302,906✔
2296
      pStmt->sql.siInfo.pVgroupHash = NULL;
7,304,828✔
2297
      pStmt->sql.siInfo.pVgroupList = NULL;
7,304,828✔
2298
    } else {
2299
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
130✔
2300
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
702✔
2301

2302
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
702✔
2303

2304
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
702✔
2305
    }
2306
  }
2307

2308
  pStmt->asyncResultAvailable = false;
7,305,144✔
2309
  SRequestObj*      pRequest = pStmt->exec.pRequest;
7,304,882✔
2310
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
7,304,882✔
2311
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
7,304,337✔
2312
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2313

2314
  if (!fp) {
7,305,190✔
2315
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
7,305,190✔
2316

2317
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
7,302,685✔
2318
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
669✔
2319
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
669✔
2320
      if (code) {
×
2321
        pStmt->exec.pRequest->code = code;
×
2322

2323
      } else {
2324
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2325
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2326
      }
2327
    }
2328

2329
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
7,302,823✔
2330

2331
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
7,302,554✔
2332
    if (affected_rows) {
7,305,862✔
2333
      *affected_rows = pStmt->exec.affectedRows;
7,303,269✔
2334
    }
2335
    pStmt->affectedRows += pStmt->exec.affectedRows;
7,304,786✔
2336

2337
    // wait for stmt bind thread to finish
2338
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
7,305,593✔
UNCOV
2339
      taosUsleep(1);
×
2340
    }
2341

2342
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
7,302,793✔
2343

2344
    ++pStmt->sql.runTimes;
7,304,053✔
2345
  } else {
2346
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
2347
    if (pWrapper == NULL) {
×
2348
      code = terrno;
×
2349
    } else {
2350
      pWrapper->pRequest = pRequest;
×
2351
      pRequest->pWrapper = pWrapper;
×
2352
    }
2353
    if (TSDB_CODE_SUCCESS == code) {
×
2354
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
2355
    }
2356
    pRequest->syncQuery = false;
×
2357
    pRequest->body.queryFp = asyncQueryCb;
×
2358
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
2359

2360
    pStmt->execSemWaited = false;
×
2361
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
2362
  }
2363

2364
_return:
7,303,784✔
2365
  if (code) {
7,303,784✔
2366
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2367
  }
2368
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
7,303,798✔
2369

2370
  STMT_RET(code);
7,304,336✔
2371
}
2372

2373
int stmtClose2(TAOS_STMT2* stmt) {
20,594✔
2374
  STscStmt2* pStmt = (STscStmt2*)stmt;
20,594✔
2375

2376
  STMT2_DLOG_E("start to close stmt");
20,594✔
2377
  taosMemoryFreeClear(pStmt->db);
20,594✔
2378

2379
  if (pStmt->bindThreadInUse) {
20,594✔
2380
    // wait for stmt bind thread to finish
2381
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
19,650✔
2382
      taosUsleep(1);
×
2383
    }
2384

2385
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
19,650✔
2386
    pStmt->queue.stopQueue = true;
19,650✔
2387
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
19,650✔
2388
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
19,650✔
2389

2390
    (void)taosThreadJoin(pStmt->bindThread, NULL);
19,650✔
2391
    pStmt->bindThreadInUse = false;
19,650✔
2392

2393
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
19,650✔
2394
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
19,650✔
2395
  }
2396

2397
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
20,594✔
2398
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
20,594✔
2399
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2400
  }
2401
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
20,594✔
2402

2403
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
20,594✔
2404
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
20,594✔
2405

2406
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
20,594✔
2407
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2408
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2409
    }
2410
  }
2411

2412
  STMT2_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
20,594✔
2413
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2414
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2415
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2416
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2417
             pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2418
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2419
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2420
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2421
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2422
  if (pStmt->sql.stbInterlaceMode) {
20,594✔
2423
    pStmt->bInfo.tagsCached = false;
19,650✔
2424
  }
2425
  pStmt->bInfo.boundColsCached = false;
20,594✔
2426

2427
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
20,594✔
2428

2429
  if (pStmt->options.asyncExecFn) {
20,594✔
2430
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
2431
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2432
    }
2433
  }
2434
  taosMemoryFree(stmt);
20,594✔
2435

2436
  return TSDB_CODE_SUCCESS;
20,594✔
2437
}
2438

2439
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
2440
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2441

2442
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
2443
    return (char*)tstrerror(terrno);
×
2444
  }
2445

2446
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2447
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
×
2448
    pStmt->exec.pRequest->code = terrno;
×
2449
  }
2450

2451
  SRequestObj* pRequest = pStmt->exec.pRequest;
×
2452
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
×
2453
    return pRequest->msgBuf;
×
2454
  }
2455
  return (const char*)tstrerror(pRequest->code);
×
2456
}
2457
/*
2458
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2459

2460
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2461
*/
2462

2463
int stmtParseColFields2(TAOS_STMT2* stmt) {
691✔
2464
  int32_t    code = 0;
691✔
2465
  STscStmt2* pStmt = (STscStmt2*)stmt;
691✔
2466
  int32_t    preCode = pStmt->errCode;
691✔
2467

2468
  STMT2_DLOG_E("start to get col fields for insert");
691✔
2469

2470
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
691✔
2471
    return pStmt->errCode;
×
2472
  }
2473

2474
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
691✔
2475
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2476
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2477
  }
2478

2479
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
691✔
2480

2481
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
691✔
2482
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2483
    pStmt->bInfo.needParse = false;
×
2484
  }
2485
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
691✔
2486
    pStmt->bInfo.needParse = false;
×
2487
  }
2488

2489
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
691✔
2490

2491
  if (pStmt->bInfo.needParse) {
691✔
2492
    STMT_ERRI_JRET(stmtParseSql(pStmt));
691✔
2493
  }
2494

2495
_return:
691✔
2496
  // compatible with previous versions
2497
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
691✔
2498
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2499
  }
2500

2501
  pStmt->errCode = preCode;
691✔
2502

2503
  return code;
691✔
2504
}
2505

2506
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
691✔
2507
  int32_t code = stmtParseColFields2(stmt);
691✔
2508
  if (code != TSDB_CODE_SUCCESS) {
691✔
2509
    return code;
×
2510
  }
2511

2512
  return stmtFetchStbColFields2(stmt, nums, fields);
691✔
2513
}
2514

2515
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2516
  int32_t    code = 0;
×
2517
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2518
  int32_t    preCode = pStmt->errCode;
×
2519

2520
  STMT2_DLOG_E("start to get param num for query");
×
2521

2522
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2523
    return pStmt->errCode;
×
2524
  }
2525

2526
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2527

2528
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2529
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2530
    pStmt->bInfo.needParse = false;
×
2531
  }
2532

2533
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2534
    resetRequest(pStmt);
×
2535
  }
2536

2537
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2538

2539
  if (pStmt->bInfo.needParse) {
×
2540
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2541
  }
2542

2543
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2544
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2545
  } else {
2546
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2547
  }
2548

2549
  STMT2_TLOG("get param num success, nums:%d", *nums);
×
2550

2551
_return:
×
2552

2553
  pStmt->errCode = preCode;
×
2554

2555
  return code;
×
2556
}
2557

2558
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
242✔
2559
  STscStmt2* pStmt = (STscStmt2*)stmt;
242✔
2560

2561
  STMT2_TLOG_E("start to use result");
242✔
2562

2563
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
242✔
2564
    STMT2_ELOG_E("useResult only for query statement");
×
2565
    return NULL;
×
2566
  }
2567

2568
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
242✔
2569
    STMT2_ELOG_E("use result after callBackFn return");
×
2570
    return NULL;
×
2571
  }
2572

2573
  return pStmt->exec.pRequest;
242✔
2574
}
2575

2576
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2577
  qInfo("async stmt bind thread started");
×
2578

2579
  ThreadArgs* targs = (ThreadArgs*)args;
×
2580
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2581

2582
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2583
  targs->fp(targs->param, NULL, code);
×
2584
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2585
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2586
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2587
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2588
  taosMemoryFree(args);
×
2589

2590
  qInfo("async stmt bind thread stopped");
×
2591

2592
  return code;
×
2593
}
2594

2595
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
×
2596
  if (pStmt == NULL || msg == NULL) {
×
2597
    return;
×
2598
  }
2599

2600
  if (pStmt->exec.pRequest == NULL) {
×
2601
    return;
×
2602
  }
2603

2604
  if (pStmt->exec.pRequest->msgBuf == NULL) {
×
2605
    return;
×
2606
  }
2607

2608
  size_t msgLen = strlen(msg);
×
2609
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
×
2610

2611
  if (msgLen >= bufLen) {
×
2612
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2613
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
2614
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2615
  } else {
2616
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
×
2617
    pStmt->exec.pRequest->msgBufLen = msgLen;
×
2618
  }
2619

2620
  return;
×
2621
}
2622

2623
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
×
2624
  stmtBuildErrorMsg(pStmt, msg);
×
2625
  pStmt->errCode = errorCode;
×
2626

2627
  return errorCode;
×
2628
}
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