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

taosdata / TDengine / #4897

25 Dec 2025 10:17AM UTC coverage: 65.717% (-0.2%) from 65.929%
#4897

push

travis-ci

web-flow
fix: [6622889291] Fix invalid rowSize. (#34043)

186011 of 283047 relevant lines covered (65.72%)

113853896.64 hits per line

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

62.4
/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) {
5,568,535✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
5,569,575✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
5,568,814✔
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;
5,568,810✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
5,572,547✔
42
  int i = 0;
5,572,547✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
26,487,414✔
44
    if (pStmt->queue.stopQueue) {
20,933,476✔
45
      return false;
17,270✔
46
    }
47
    if (i < 10) {
20,917,210✔
48
      taosUsleep(1);
19,491,914✔
49
      i++;
19,489,956✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,425,296✔
52
      if (pStmt->queue.stopQueue) {
1,424,942✔
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
1,424,942✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
1,424,756✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,424,880✔
60
    }
61
  }
62

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

67
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
5,554,850✔
68
  if (pStmt->queue.head == pStmt->queue.tail) {
5,556,158✔
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;
5,555,401✔
76
  pStmt->queue.head->next = node->next;
5,555,652✔
77
  if (pStmt->queue.tail == node) {
5,556,158✔
78
    pStmt->queue.tail = pStmt->queue.head;
3,441,541✔
79
  }
80
  node->next = NULL;
5,555,907✔
81
  *param = node;
5,555,907✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
5,555,907✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
5,556,127✔
85

86
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
5,555,721✔
87

88
  return true;
5,555,656✔
89
}
90

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

97
  param->next = NULL;
5,551,919✔
98

99
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
5,551,919✔
100

101
  pStmt->queue.tail->next = param;
5,554,373✔
102
  pStmt->queue.tail = param;
5,554,122✔
103
  pStmt->stat.bindDataNum++;
5,553,365✔
104

105
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
5,554,122✔
106
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
5,553,802✔
107

108
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
5,554,481✔
109

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

114
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
5,201,424✔
115
  int32_t code = 0;
5,201,424✔
116

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

136
  return code;
5,201,548✔
137
}
138

139
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
12,621,241✔
140
  int32_t code = 0;
12,621,241✔
141

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
12,621,241✔
143
    STMT2_LOG_SEQ(newStatus);
12,624,497✔
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
12,622,567✔
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) {
12,619,236✔
152
    case STMT_PREPARE:
17,379✔
153
      pStmt->errCode = 0;
17,379✔
154
      break;
13,861✔
155
    case STMT_SETTBNAME:
3,648,684✔
156
      if (STMT_STATUS_EQ(INIT)) {
3,648,684✔
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
3,649,700✔
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
3,648,943✔
163
    case STMT_SETTAGS:
1,499,986✔
164
      if (STMT_STATUS_EQ(INIT)) {
1,499,986✔
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
1,499,986✔
168
    case STMT_FETCH_FIELDS:
77✔
169
      if (STMT_STATUS_EQ(INIT)) {
77✔
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
77✔
173
    case STMT_BIND:
3,644,433✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
3,644,433✔
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;
3,650,262✔
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:
1,903,860✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
1,903,860✔
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
1,905,115✔
193
    case STMT_EXECUTE:
1,904,817✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,904,817✔
195
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
29✔
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)) {
1,904,282✔
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
202
        }
203
      }
204
      break;
1,904,562✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
12,622,806✔
211

212
  pStmt->sql.status = newStatus;
12,622,806✔
213

214
  return TSDB_CODE_SUCCESS;
12,623,065✔
215
}
216

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

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
17,427✔
221

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

227
  *tbName = pStmt->bInfo.tbName;
17,350✔
228

229
  return TSDB_CODE_SUCCESS;
17,350✔
230
}
231

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

241
  if ((tags != NULL && ((SBoundColInfo*)tags)->numOfCols == 0) || !autoCreateTbl) {
17,427✔
242
    pStmt->sql.autoCreateTbl = false;
16,841✔
243
  }
244

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

249
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
16,925✔
250
  pStmt->bInfo.tbSuid = pTableMeta->suid;
16,965✔
251
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
17,176✔
252
  pStmt->bInfo.tbType = pTableMeta->tableType;
16,965✔
253

254
  if (!pStmt->bInfo.tagsCached) {
17,427✔
255
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
16,925✔
256
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
17,427✔
257
  }
258

259
  if (cols) {
17,176✔
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;
17,176✔
275
  }
276
  pStmt->bInfo.boundTags = tags;
16,925✔
277
  pStmt->bInfo.tagsCached = false;
16,925✔
278
  pStmt->bInfo.tbNameFlag = tbNameFlag;
16,459✔
279
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
17,172✔
280

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

285
  return TSDB_CODE_SUCCESS;
17,176✔
286
}
287

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

291
  pStmt->sql.pVgHash = pVgHash;
17,427✔
292
  pStmt->exec.pBlockHash = pBlockHash;
17,427✔
293

294
  return TSDB_CODE_SUCCESS;
17,176✔
295
}
296

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

302
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
17,427✔
303
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
16,921✔
304

305
  pStmt->sql.autoCreateTbl = autoCreateTbl;
17,176✔
306

307
  return TSDB_CODE_SUCCESS;
17,427✔
308
}
309

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

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

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

319
  return TSDB_CODE_SUCCESS;
77✔
320
}
321

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

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

332
  STMT_ERR_RET(stmtCreateRequest(pStmt));
17,456✔
333
  pStmt->exec.pRequest->stmtBindVersion = 2;
17,456✔
334

335
  pStmt->stat.parseSqlNum++;
17,456✔
336

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

340
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
17,456✔
341

342
  pStmt->bInfo.needParse = false;
17,456✔
343

344
  if (pStmt->sql.type == 0) {
16,994✔
345
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
29✔
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))) {
29✔
349
      pStmt->sql.type = STMT_TYPE_QUERY;
29✔
350
      pStmt->sql.stbInterlaceMode = false;
29✔
351

352
      return TSDB_CODE_SUCCESS;
29✔
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) {
16,714✔
361
    pStmt->sql.stbInterlaceMode = false;
×
362
    return TSDB_CODE_SUCCESS;
×
363
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
16,925✔
364
    pStmt->sql.stbInterlaceMode = false;
×
365
  }
366

367
  STableDataCxt** pSrc =
368
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
16,965✔
369
  if (NULL == pSrc || NULL == *pSrc) {
17,427✔
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;
17,427✔
375
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
17,427✔
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) {
17,682✔
395
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
17,099✔
396
    if (NULL == pStmt->sql.pBindInfo) {
17,350✔
397
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
398
      return terrno;
×
399
    }
400
  }
401

402
  return TSDB_CODE_SUCCESS;
17,427✔
403
}
404

405
static int32_t stmtPrintBindv(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bindv, int32_t col_idx, bool isTags) {
×
406
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
407
  int32_t    count = 0;
×
408
  int32_t    code = 0;
×
409

410
  if (bindv == NULL) {
×
411
    STMT2_TLOG("bindv is NULL, col_idx:%d, isTags:%d", col_idx, isTags);
×
412
    return TSDB_CODE_SUCCESS;
×
413
  }
414

415
  if (col_idx >= 0) {
×
416
    count = 1;
×
417
    STMT2_TLOG("single col bind, col_idx:%d", col_idx);
×
418
  } else {
419
    if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
×
420
        (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
×
421
      if (pStmt->sql.placeholderOfTags == 0 && pStmt->sql.placeholderOfCols == 0) {
×
422
        code = stmtGetStbColFields2(pStmt, NULL, NULL);
×
423
        if (code != TSDB_CODE_SUCCESS) {
×
424
          return code;
×
425
        }
426
      }
427
      if (isTags) {
×
428
        count = pStmt->sql.placeholderOfTags;
×
429
        STMT2_TLOG("print tags bindv, cols:%d", count);
×
430
      } else {
431
        count = pStmt->sql.placeholderOfCols;
×
432
        STMT2_TLOG("print cols bindv, cols:%d", count);
×
433
      }
434
    } else if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
435
      count = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
436
      STMT2_TLOG("print query bindv, cols:%d", count);
×
437
    }
438
  }
439

440
  if (code != TSDB_CODE_SUCCESS) {
×
441
    STMT2_ELOG("failed to get param count, code:%d", code);
×
442
    return code;
×
443
  }
444

445
  for (int i = 0; i < count; i++) {
×
446
    int32_t type = bindv[i].buffer_type;
×
447
    int32_t num = bindv[i].num;
×
448
    char*   current_buf = (char*)bindv[i].buffer;
×
449

450
    for (int j = 0; j < num; j++) {
×
451
      char    buf[256] = {0};
×
452
      int32_t len = 0;
×
453
      bool    isNull = (bindv[i].is_null && bindv[i].is_null[j]);
×
454

455
      if (IS_VAR_DATA_TYPE(type) && bindv[i].length) {
×
456
        len = bindv[i].length[j];
×
457
      } else {
458
        len = tDataTypes[type].bytes;
×
459
      }
460

461
      if (isNull) {
×
462
        snprintf(buf, sizeof(buf), "NULL");
×
463
      } else {
464
        if (current_buf == NULL) {
×
465
          snprintf(buf, sizeof(buf), "NULL(Buf)");
×
466
        } else {
467
          switch (type) {
×
468
            case TSDB_DATA_TYPE_BOOL:
×
469
              snprintf(buf, sizeof(buf), "%d", *(int8_t*)current_buf);
×
470
              break;
×
471
            case TSDB_DATA_TYPE_TINYINT:
×
472
              snprintf(buf, sizeof(buf), "%d", *(int8_t*)current_buf);
×
473
              break;
×
474
            case TSDB_DATA_TYPE_SMALLINT:
×
475
              snprintf(buf, sizeof(buf), "%d", *(int16_t*)current_buf);
×
476
              break;
×
477
            case TSDB_DATA_TYPE_INT:
×
478
              snprintf(buf, sizeof(buf), "%d", *(int32_t*)current_buf);
×
479
              break;
×
480
            case TSDB_DATA_TYPE_BIGINT:
×
481
              snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t*)current_buf);
×
482
              break;
×
483
            case TSDB_DATA_TYPE_FLOAT:
×
484
              snprintf(buf, sizeof(buf), "%f", *(float*)current_buf);
×
485
              break;
×
486
            case TSDB_DATA_TYPE_DOUBLE:
×
487
              snprintf(buf, sizeof(buf), "%f", *(double*)current_buf);
×
488
              break;
×
489
            case TSDB_DATA_TYPE_BINARY:
×
490
            case TSDB_DATA_TYPE_NCHAR:
491
            case TSDB_DATA_TYPE_GEOMETRY:
492
            case TSDB_DATA_TYPE_VARBINARY:
493
              snprintf(buf, sizeof(buf), "len:%d, val:%.*s", len, len, current_buf);
×
494
              break;
×
495
            case TSDB_DATA_TYPE_TIMESTAMP:
×
496
              snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t*)current_buf);
×
497
              break;
×
498
            case TSDB_DATA_TYPE_UTINYINT:
×
499
              snprintf(buf, sizeof(buf), "%u", *(uint8_t*)current_buf);
×
500
              break;
×
501
            case TSDB_DATA_TYPE_USMALLINT:
×
502
              snprintf(buf, sizeof(buf), "%u", *(uint16_t*)current_buf);
×
503
              break;
×
504
            case TSDB_DATA_TYPE_UINT:
×
505
              snprintf(buf, sizeof(buf), "%u", *(uint32_t*)current_buf);
×
506
              break;
×
507
            case TSDB_DATA_TYPE_UBIGINT:
×
508
              snprintf(buf, sizeof(buf), "%" PRIu64, *(uint64_t*)current_buf);
×
509
              break;
×
510
            default:
×
511
              snprintf(buf, sizeof(buf), "UnknownType:%d", type);
×
512
              break;
×
513
          }
514
        }
515
      }
516

517
      STMT2_TLOG("bindv[%d] row[%d]: type:%s, val:%s", i, j, tDataTypes[type].name, buf);
×
518

519
      if (!isNull && current_buf) {
×
520
        current_buf += len;
×
521
      }
522
    }
523
  }
524

525
  return TSDB_CODE_SUCCESS;
×
526
}
527

528
static void resetRequest(STscStmt2* pStmt) {
17,707✔
529
  if (pStmt->exec.pRequest) {
17,707✔
530
    taos_free_result(pStmt->exec.pRequest);
17,379✔
531
    pStmt->exec.pRequest = NULL;
17,379✔
532
  }
533
  pStmt->asyncResultAvailable = false;
17,707✔
534
}
17,707✔
535

536
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
1,939,484✔
537
  pStmt->bInfo.tbUid = 0;
1,939,484✔
538
  pStmt->bInfo.tbVgId = -1;
1,939,484✔
539
  pStmt->bInfo.tbType = 0;
1,939,233✔
540
  pStmt->bInfo.needParse = true;
1,939,233✔
541
  pStmt->bInfo.inExecCache = false;
1,939,484✔
542

543
  pStmt->bInfo.tbName[0] = 0;
1,939,233✔
544
  pStmt->bInfo.tbFName[0] = 0;
1,939,233✔
545
  if (!pStmt->bInfo.tagsCached) {
1,938,476✔
546
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
1,439,383✔
547
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
1,439,955✔
548
  }
549

550
  if (!pStmt->bInfo.boundColsCached) {
1,938,793✔
551
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
34,339✔
552
    pStmt->bInfo.boundCols = NULL;
34,339✔
553
  }
554

555
  if (!pStmt->sql.autoCreateTbl) {
1,938,538✔
556
    pStmt->bInfo.stbFName[0] = 0;
1,439,747✔
557
    pStmt->bInfo.tbSuid = 0;
1,440,253✔
558
  }
559

560
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
1,937,530✔
561
             pStmt->sql.autoCreateTbl);
562

563
  return TSDB_CODE_SUCCESS;
1,938,101✔
564
}
565

566
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
567
  (void)qResetStmtColumns(pTb->aCol, true);
×
568
  taosArrayDestroy(pTb->aCol);
×
569
}
×
570

571
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
1,902,919✔
572
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
1,902,919✔
573
  if (NULL == pTblBuf->pCurBuff) {
1,904,202✔
574
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
372✔
575
    return;
×
576
  }
577
  pTblBuf->buffIdx = 1;
1,904,336✔
578
  pTblBuf->buffOffset = sizeof(*pQueue->head);
1,904,081✔
579

580
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
1,904,081✔
581
  pQueue->qRemainNum = 0;
1,903,579✔
582
  pQueue->head->next = NULL;
1,903,834✔
583
}
584

585
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
1,920,066✔
586
  if (pStmt->sql.stbInterlaceMode) {
1,920,066✔
587
    if (deepClean) {
1,920,347✔
588
      taosHashCleanup(pStmt->exec.pBlockHash);
17,270✔
589
      pStmt->exec.pBlockHash = NULL;
17,270✔
590

591
      if (NULL != pStmt->exec.pCurrBlock) {
17,270✔
592
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
17,270✔
593
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
17,270✔
594
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
17,270✔
595
        pStmt->exec.pCurrBlock = NULL;
17,270✔
596
      }
597
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
17,270✔
598
        resetRequest(pStmt);
17,270✔
599
      }
600
    } else {
601
      pStmt->sql.siInfo.pTableColsIdx = 0;
1,903,077✔
602
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
1,903,579✔
603
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
1,902,382✔
604
    }
605
    if (NULL != pStmt->exec.pRequest) {
1,922,229✔
606
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
1,904,959✔
607
    }
608
  } else {
609
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
753✔
610
      resetRequest(pStmt);
×
611
    }
612

613
    size_t keyLen = 0;
218✔
614
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
218✔
615
    while (pIter) {
895✔
616
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
677✔
617
      char*          key = taosHashGetKey(pIter, &keyLen);
677✔
618
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
677✔
619

620
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
677✔
621
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
80✔
622
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
189✔
623

624
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
80✔
625
        continue;
80✔
626
      }
627

628
      qDestroyStmtDataBlock(pBlocks);
597✔
629
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
597✔
630

631
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
597✔
632
    }
633

634
    if (keepTable) {
218✔
635
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
109✔
636
                 keepTable, deepClean);
637
      return TSDB_CODE_SUCCESS;
109✔
638
    }
639

640
    taosHashCleanup(pStmt->exec.pBlockHash);
109✔
641
    pStmt->exec.pBlockHash = NULL;
109✔
642

643
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
109✔
644
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
109✔
645
  }
646

647
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
1,922,338✔
648
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
1,921,082✔
649
             keepTable, deepClean);
650

651
  return TSDB_CODE_SUCCESS;
1,920,738✔
652
}
653

654
static void stmtFreeTbBuf(void* buf) {
17,270✔
655
  void* pBuf = *(void**)buf;
17,270✔
656
  taosMemoryFree(pBuf);
17,270✔
657
}
17,270✔
658

659
static void stmtFreeTbCols(void* buf) {
17,270,000✔
660
  SArray* pCols = *(SArray**)buf;
17,270,000✔
661
  taosArrayDestroy(pCols);
17,270,000✔
662
}
17,270,000✔
663

664
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
17,131✔
665
  STMT2_TLOG_E("start to free SQL info");
17,131✔
666

667
  taosMemoryFree(pStmt->sql.pBindInfo);
17,131✔
668
  taosMemoryFree(pStmt->sql.queryRes.fields);
17,131✔
669
  taosMemoryFree(pStmt->sql.queryRes.userFields);
17,131✔
670
  taosMemoryFree(pStmt->sql.sqlStr);
17,131✔
671
  qDestroyQuery(pStmt->sql.pQuery);
17,131✔
672
  taosArrayDestroy(pStmt->sql.nodeList);
17,131✔
673
  taosHashCleanup(pStmt->sql.pVgHash);
17,131✔
674
  pStmt->sql.pVgHash = NULL;
17,131✔
675
  if (pStmt->sql.fixValueTags) {
17,131✔
676
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
62✔
677
  }
678

679
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
17,131✔
680
  while (pIter) {
17,211✔
681
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
80✔
682

683
    qDestroyStmtDataBlock(pCache->pDataCtx);
80✔
684
    qDestroyBoundColInfo(pCache->boundTags);
80✔
685
    taosMemoryFreeClear(pCache->boundTags);
80✔
686

687
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
80✔
688
  }
689
  taosHashCleanup(pStmt->sql.pTableCache);
17,131✔
690
  pStmt->sql.pTableCache = NULL;
17,131✔
691

692
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
17,131✔
693
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
17,131✔
694

695
  taos_free_result(pStmt->sql.siInfo.pRequest);
17,131✔
696
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
17,131✔
697
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
17,131✔
698
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
17,131✔
699
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
17,131✔
700
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
17,131✔
701
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
17,131✔
702
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
17,131✔
703
  pStmt->sql.siInfo.pTableCols = NULL;
17,131✔
704

705
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
17,131✔
706
  pStmt->sql.siInfo.tableColsReady = true;
17,131✔
707

708
  STMT2_TLOG_E("end to free SQL info");
17,131✔
709

710
  return TSDB_CODE_SUCCESS;
17,131✔
711
}
712

713
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
1,500,123✔
714
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
1,500,123✔
715
    return TSDB_CODE_SUCCESS;
×
716
  }
717

718
  SVgroupInfo      vgInfo = {0};
1,500,123✔
719
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1,500,123✔
720
                           .requestId = pStmt->exec.pRequest->requestId,
1,500,123✔
721
                           .requestObjRefId = pStmt->exec.pRequest->self,
1,500,123✔
722
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1,500,123✔
723

724
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
1,500,743✔
725
  if (TSDB_CODE_SUCCESS != code) {
1,500,433✔
726
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
727
    return code;
×
728
  }
729

730
  code =
731
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
1,500,433✔
732
  if (TSDB_CODE_SUCCESS != code) {
1,500,805✔
733
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
124✔
734
    return code;
×
735
  }
736

737
  *vgId = vgInfo.vgId;
1,500,681✔
738

739
  return TSDB_CODE_SUCCESS;
1,500,681✔
740
}
741

742
int32_t stmtGetTableMetaAndValidate(STscStmt2* pStmt, uint64_t* uid, uint64_t* suid, int32_t* vgId, int8_t* tableType) {
16,841✔
743
  STableMeta*      pTableMeta = NULL;
16,841✔
744
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
16,841✔
745
                           .requestId = pStmt->exec.pRequest->requestId,
16,841✔
746
                           .requestObjRefId = pStmt->exec.pRequest->self,
16,841✔
747
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
16,841✔
748
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
16,630✔
749

750
  pStmt->stat.ctgGetTbMetaNum++;
16,841✔
751

752
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
16,841✔
753
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
×
754
    (void)stmtCleanBindInfo(pStmt);
×
755

756
    if (!pStmt->sql.autoCreateTbl) {
×
757
      STMT2_ELOG("table %s does not exist and autoCreateTbl is disabled", pStmt->bInfo.tbFName);
×
758
      STMT_ERR_RET(TSDB_CODE_PAR_TABLE_NOT_EXIST);
×
759
    }
760

761
    STMT_ERR_RET(code);
×
762
  }
763

764
  STMT_ERR_RET(code);
16,841✔
765

766
  *uid = pTableMeta->uid;
16,841✔
767
  *suid = pTableMeta->suid;
16,841✔
768
  *tableType = pTableMeta->tableType;
16,841✔
769
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
16,841✔
770
  *vgId = pTableMeta->vgId;
16,590✔
771

772
  taosMemoryFree(pTableMeta);
16,590✔
773

774
  return TSDB_CODE_SUCCESS;
16,590✔
775
}
776

777
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
517✔
778
                                    uint64_t suid, int32_t vgId) {
779
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
517✔
780
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
517✔
781

782
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
517✔
783

784
  return TSDB_CODE_SUCCESS;
517✔
785
}
786

787
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
17,867✔
788
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
17,867✔
789
    pStmt->bInfo.needParse = false;
×
790
    pStmt->bInfo.inExecCache = false;
×
791
    return TSDB_CODE_SUCCESS;
×
792
  }
793

794
  pStmt->bInfo.needParse = true;
17,616✔
795
  pStmt->bInfo.inExecCache = false;
17,110✔
796

797
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
17,867✔
798
  if (pCxtInExec) {
17,867✔
799
    pStmt->bInfo.needParse = false;
×
800
    pStmt->bInfo.inExecCache = true;
×
801

802
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
803

804
    if (pStmt->sql.autoCreateTbl) {
×
805
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
806
      return TSDB_CODE_SUCCESS;
×
807
    }
808
  }
809

810
  if (NULL == pStmt->pCatalog) {
17,867✔
811
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
17,102✔
812
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
16,596✔
813
  }
814

815
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
17,361✔
816
    if (pStmt->bInfo.inExecCache) {
17,095✔
817
      pStmt->bInfo.needParse = false;
×
818
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
819
      return TSDB_CODE_SUCCESS;
×
820
    }
821

822
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
17,350✔
823

824
    return TSDB_CODE_SUCCESS;
17,350✔
825
  }
826

827
  if (pStmt->sql.autoCreateTbl) {
517✔
828
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
517✔
829
    if (pCache) {
517✔
830
      pStmt->bInfo.needParse = false;
517✔
831
      pStmt->bInfo.tbUid = 0;
517✔
832

833
      STableDataCxt* pNewBlock = NULL;
517✔
834
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
517✔
835

836
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
517✔
837
                      POINTER_BYTES)) {
838
        STMT_ERR_RET(terrno);
×
839
      }
840

841
      pStmt->exec.pCurrBlock = pNewBlock;
517✔
842

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

845
      return TSDB_CODE_SUCCESS;
517✔
846
    }
847

848
    STMT_RET(stmtCleanBindInfo(pStmt));
×
849
  }
850

851
  uint64_t uid, suid;
×
852
  int32_t  vgId;
×
853
  int8_t   tableType;
×
854

855
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
×
856

857
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
858

859
  if (uid == pStmt->bInfo.tbUid) {
×
860
    pStmt->bInfo.needParse = false;
×
861

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

864
    return TSDB_CODE_SUCCESS;
×
865
  }
866

867
  if (pStmt->bInfo.inExecCache) {
×
868
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
869
    if (NULL == pCache) {
×
870
      STMT2_ELOG("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
871
                 pStmt->bInfo.tbFName, uid, cacheUid);
872

873
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
874
    }
875

876
    pStmt->bInfo.needParse = false;
×
877

878
    pStmt->bInfo.tbUid = uid;
×
879
    pStmt->bInfo.tbSuid = suid;
×
880
    pStmt->bInfo.tbType = tableType;
×
881
    pStmt->bInfo.boundTags = pCache->boundTags;
×
882
    pStmt->bInfo.tagsCached = true;
×
883

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

886
    return TSDB_CODE_SUCCESS;
×
887
  }
888

889
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
890
  if (pCache) {
×
891
    pStmt->bInfo.needParse = false;
×
892

893
    pStmt->bInfo.tbUid = uid;
×
894
    pStmt->bInfo.tbSuid = suid;
×
895
    pStmt->bInfo.tbType = tableType;
×
896
    pStmt->bInfo.boundTags = pCache->boundTags;
×
897
    pStmt->bInfo.tagsCached = true;
×
898

899
    STableDataCxt* pNewBlock = NULL;
×
900
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
901

902
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
903
                    POINTER_BYTES)) {
904
      STMT_ERR_RET(terrno);
×
905
    }
906

907
    pStmt->exec.pCurrBlock = pNewBlock;
×
908

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

911
    return TSDB_CODE_SUCCESS;
×
912
  }
913

914
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
915

916
  return TSDB_CODE_SUCCESS;
×
917
}
918

919
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
920
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
921

922
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
923
  if (NULL == pStmt->sql.pTableCache) {
×
924
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
925
    STMT_ERR_RET(terrno);
×
926
  }
927

928
  pStmt->sql.status = STMT_INIT;
×
929

930
  return TSDB_CODE_SUCCESS;
×
931
}
932

933
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
5,555,184✔
934
  SStmtQNode* pParam = (SStmtQNode*)param;
5,555,184✔
935

936
  if (pParam->restoreTbCols) {
5,555,184✔
937
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
5,555,215✔
938
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
3,650,982✔
939
      *p = taosArrayInit(20, POINTER_BYTES);
3,650,727✔
940
      if (*p == NULL) {
3,650,418✔
941
        pStmt->errCode = terrno;
×
942
      }
943
    }
944
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
1,904,739✔
945
    STMT2_TLOG_E("restore pTableCols finished");
1,905,021✔
946
  } else {
947
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
3,650,955✔
948
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
949
    // taosMemoryFree(pParam->pTbData);
950
    if (code != TSDB_CODE_SUCCESS) {
3,650,348✔
951
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
952
      pStmt->errCode = code;
×
953
    }
954
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
3,650,348✔
955
  }
956
}
5,555,938✔
957

958
static void* stmtBindThreadFunc(void* param) {
17,270✔
959
  setThreadName("stmt2Bind");
17,270✔
960

961
  STscStmt2* pStmt = (STscStmt2*)param;
17,270✔
962
  STMT2_DLOG_E("stmt2 bind thread started");
17,270✔
963

964
  while (true) {
5,555,907✔
965
    SStmtQNode* asyncParam = NULL;
5,573,177✔
966

967
    if (!stmtDequeue(pStmt, &asyncParam)) {
5,572,922✔
968
      if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
17,270✔
969
        STMT2_DLOG_E("queue is empty and stopQueue is set, thread will exit");
17,270✔
970
        break;
17,270✔
971
      }
972
      continue;
×
973
    }
974

975
    stmtAsyncOutput(pStmt, asyncParam);
5,555,246✔
976
  }
977

978
  STMT2_DLOG_E("stmt2 bind thread stopped");
17,270✔
979
  return NULL;
17,270✔
980
}
981

982
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
17,270✔
983
  TdThreadAttr thAttr;
16,685✔
984
  if (taosThreadAttrInit(&thAttr) != 0) {
17,270✔
985
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
986
  }
987
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
17,270✔
988
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
989
  }
990

991
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
17,270✔
992
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
993
    STMT_ERR_RET(terrno);
×
994
  }
995

996
  pStmt->bindThreadInUse = true;
17,270✔
997

998
  (void)taosThreadAttrDestroy(&thAttr);
17,270✔
999
  return TSDB_CODE_SUCCESS;
17,270✔
1000
}
1001

1002
static int32_t stmtInitQueue(STscStmt2* pStmt) {
17,270✔
1003
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
17,270✔
1004
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
17,270✔
1005
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
34,540✔
1006
  pStmt->queue.tail = pStmt->queue.head;
17,270✔
1007

1008
  return TSDB_CODE_SUCCESS;
17,270✔
1009
}
1010

1011
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
17,131✔
1012
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
17,131✔
1013
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
17,131✔
1014
  pStmt->asyncBindParam.asyncBindNum = 0;
17,131✔
1015

1016
  return TSDB_CODE_SUCCESS;
17,131✔
1017
}
1018

1019
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
17,270✔
1020
  pTblBuf->buffUnit = sizeof(SStmtQNode);
17,270✔
1021
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
17,270✔
1022
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
17,270✔
1023
  if (NULL == pTblBuf->pBufList) {
17,270✔
1024
    return terrno;
×
1025
  }
1026
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
17,270✔
1027
  if (NULL == buff) {
17,270✔
1028
    return terrno;
×
1029
  }
1030

1031
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
34,540✔
1032
    return terrno;
×
1033
  }
1034

1035
  pTblBuf->pCurBuff = buff;
17,270✔
1036
  pTblBuf->buffIdx = 1;
17,270✔
1037
  pTblBuf->buffOffset = 0;
17,270✔
1038

1039
  return TSDB_CODE_SUCCESS;
17,270✔
1040
}
1041

1042
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
17,131✔
1043
  STscObj*   pObj = (STscObj*)taos;
17,131✔
1044
  STscStmt2* pStmt = NULL;
17,131✔
1045
  int32_t    code = 0;
17,131✔
1046

1047
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
17,131✔
1048
  if (NULL == pStmt) {
17,131✔
1049
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
1050
    return NULL;
×
1051
  }
1052

1053
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
17,131✔
1054
  if (NULL == pStmt->sql.pTableCache) {
17,131✔
1055
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtInit2:%s", tstrerror(terrno));
×
1056
    taosMemoryFree(pStmt);
×
1057
    return NULL;
×
1058
  }
1059

1060
  pStmt->taos = pObj;
17,131✔
1061
  if (taos->db[0] != '\0') {
17,131✔
1062
    pStmt->db = taosStrdup(taos->db);
17,131✔
1063
  }
1064
  pStmt->bInfo.needParse = true;
17,131✔
1065
  pStmt->sql.status = STMT_INIT;
17,131✔
1066
  pStmt->errCode = TSDB_CODE_SUCCESS;
17,131✔
1067

1068
  if (NULL != pOptions) {
17,131✔
1069
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
17,051✔
1070
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
17,051✔
1071
      pStmt->stbInterlaceMode = true;
17,022✔
1072
    }
1073

1074
    pStmt->reqid = pOptions->reqid;
17,051✔
1075
  }
1076

1077
  if (pStmt->stbInterlaceMode) {
17,131✔
1078
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
17,022✔
1079
    pStmt->sql.siInfo.acctId = taos->acctId;
17,022✔
1080
    pStmt->sql.siInfo.dbname = taos->db;
17,022✔
1081
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
17,022✔
1082

1083
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
17,022✔
1084
    if (NULL == pStmt->sql.siInfo.pTableHash) {
17,022✔
1085
      STMT2_ELOG("fail to allocate memory for pTableHash:%s", tstrerror(terrno));
×
1086
      (void)stmtClose2(pStmt);
×
1087
      return NULL;
×
1088
    }
1089

1090
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
17,022✔
1091
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
17,022✔
1092
      STMT2_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
1093
      (void)stmtClose2(pStmt);
×
1094
      return NULL;
×
1095
    }
1096

1097
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
17,022✔
1098
    if (NULL == pStmt->sql.siInfo.pTableCols) {
17,022✔
1099
      STMT2_ELOG("fail to allocate memory for pTableCols:%s", tstrerror(terrno));
×
1100
      (void)stmtClose2(pStmt);
×
1101
      return NULL;
×
1102
    }
1103

1104
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
17,022✔
1105
    if (TSDB_CODE_SUCCESS == code) {
17,022✔
1106
      code = stmtInitQueue(pStmt);
17,022✔
1107
    }
1108
    if (TSDB_CODE_SUCCESS == code) {
17,022✔
1109
      code = stmtStartBindThread(pStmt);
17,022✔
1110
    }
1111
    if (TSDB_CODE_SUCCESS != code) {
17,022✔
1112
      terrno = code;
×
1113
      STMT2_ELOG("fail to init stmt2 bind thread:%s", tstrerror(code));
×
1114
      (void)stmtClose2(pStmt);
×
1115
      return NULL;
×
1116
    }
1117
  }
1118

1119
  pStmt->sql.siInfo.tableColsReady = true;
17,131✔
1120
  if (pStmt->options.asyncExecFn) {
17,131✔
1121
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
×
1122
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1123
      STMT2_ELOG("fail to init asyncExecSem:%s", tstrerror(terrno));
×
1124
      (void)stmtClose2(pStmt);
×
1125
      return NULL;
×
1126
    }
1127
  }
1128
  code = stmtIniAsyncBind(pStmt);
17,131✔
1129
  if (TSDB_CODE_SUCCESS != code) {
17,131✔
1130
    terrno = code;
×
1131
    STMT2_ELOG("fail to start init asyncExecSem:%s", tstrerror(code));
×
1132

1133
    (void)stmtClose2(pStmt);
×
1134
    return NULL;
×
1135
  }
1136

1137
  pStmt->execSemWaited = false;
17,131✔
1138

1139
  // STMT_LOG_SEQ(STMT_INIT);
1140

1141
  STMT2_DLOG("stmt2 initialize finished, seqId:%d, db:%s, interlaceMode:%d, asyncExec:%d", pStmt->seqId, pStmt->db,
17,131✔
1142
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1143

1144
  return pStmt;
17,131✔
1145
}
1146

1147
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
1148
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1149
  if (dbName == NULL || dbName[0] == '\0') {
×
1150
    STMT2_ELOG_E("dbname in sql is illegal");
×
1151
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
1152
  }
1153

1154
  STMT2_DLOG("dbname is specified in sql:%s", dbName);
×
1155
  if (pStmt->db == NULL || pStmt->db[0] == '\0') {
×
1156
    taosMemoryFreeClear(pStmt->db);
×
1157
    STMT2_DLOG("dbname:%s is by sql, not by taosconnect", dbName);
×
1158
    pStmt->db = taosStrdup(dbName);
×
1159
    (void)strdequote(pStmt->db);
×
1160
  }
1161
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1162

1163
  // The SQL statement specifies a database name, overriding the previously specified database
1164
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
1165
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
×
1166
  (void)strdequote(pStmt->exec.pRequest->pDb);
×
1167
  if (pStmt->exec.pRequest->pDb == NULL) {
×
1168
    return terrno;
×
1169
  }
1170
  if (pStmt->sql.stbInterlaceMode) {
×
1171
    pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
×
1172
  }
1173
  return TSDB_CODE_SUCCESS;
×
1174
}
1175
static int32_t stmtResetStbInterlaceCache(STscStmt2* pStmt) {
248✔
1176
  int32_t code = TSDB_CODE_SUCCESS;
248✔
1177

1178
  if (pStmt->bindThreadInUse) {
248✔
1179
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
248✔
1180
      taosUsleep(1);
×
1181
    }
1182
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
248✔
1183
    pStmt->queue.stopQueue = true;
248✔
1184
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
248✔
1185
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
248✔
1186

1187
    (void)taosThreadJoin(pStmt->bindThread, NULL);
248✔
1188
    pStmt->bindThreadInUse = false;
248✔
1189
    pStmt->queue.head = NULL;
248✔
1190
    pStmt->queue.tail = NULL;
248✔
1191
    pStmt->queue.qRemainNum = 0;
248✔
1192

1193
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
248✔
1194
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
248✔
1195
  }
1196

1197
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
248✔
1198
  if (NULL == pStmt->sql.siInfo.pTableHash) {
248✔
1199
    return terrno;
×
1200
  }
1201

1202
  pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
248✔
1203
  if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
248✔
1204
    return terrno;
×
1205
  }
1206

1207
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
248✔
1208
  if (NULL == pStmt->sql.siInfo.pTableCols) {
248✔
1209
    return terrno;
×
1210
  }
1211

1212
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
248✔
1213

1214
  if (TSDB_CODE_SUCCESS == code) {
248✔
1215
    code = stmtInitQueue(pStmt);
248✔
1216
    pStmt->queue.stopQueue = false;
248✔
1217
  }
1218
  if (TSDB_CODE_SUCCESS == code) {
248✔
1219
    code = stmtStartBindThread(pStmt);
248✔
1220
  }
1221
  if (TSDB_CODE_SUCCESS != code) {
248✔
1222
    return code;
×
1223
  }
1224

1225
  return TSDB_CODE_SUCCESS;
248✔
1226
}
1227

1228
static int32_t stmtDeepReset(STscStmt2* pStmt) {
248✔
1229
  char*             db = pStmt->db;
248✔
1230
  bool              stbInterlaceMode = pStmt->stbInterlaceMode;
248✔
1231
  TAOS_STMT2_OPTION options = pStmt->options;
248✔
1232
  uint32_t          reqid = pStmt->reqid;
248✔
1233

1234
  pStmt->errCode = 0;
248✔
1235
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
248✔
1236
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1237
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1238
    }
1239
    pStmt->execSemWaited = true;
×
1240
  }
1241
  pStmt->sql.autoCreateTbl = false;
248✔
1242
  taosMemoryFree(pStmt->sql.pBindInfo);
248✔
1243
  pStmt->sql.pBindInfo = NULL;
248✔
1244

1245
  taosMemoryFree(pStmt->sql.queryRes.fields);
248✔
1246
  pStmt->sql.queryRes.fields = NULL;
248✔
1247

1248
  taosMemoryFree(pStmt->sql.queryRes.userFields);
248✔
1249
  pStmt->sql.queryRes.userFields = NULL;
248✔
1250

1251
  pStmt->sql.type = 0;
248✔
1252
  pStmt->sql.runTimes = 0;
248✔
1253
  taosMemoryFree(pStmt->sql.sqlStr);
248✔
1254
  pStmt->sql.sqlStr = NULL;
248✔
1255

1256
  qDestroyQuery(pStmt->sql.pQuery);
248✔
1257
  pStmt->sql.pQuery = NULL;
248✔
1258

1259
  taosArrayDestroy(pStmt->sql.nodeList);
248✔
1260
  pStmt->sql.nodeList = NULL;
248✔
1261

1262
  taosHashCleanup(pStmt->sql.pVgHash);
248✔
1263
  pStmt->sql.pVgHash = NULL;
248✔
1264

1265
  if (pStmt->sql.fixValueTags) {
248✔
1266
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
248✔
1267
    pStmt->sql.fixValueTbReq = NULL;
248✔
1268
  }
1269
  pStmt->sql.fixValueTags = false;
248✔
1270

1271
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
248✔
1272
  while (pIter) {
248✔
1273
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
1274

1275
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
1276
    qDestroyBoundColInfo(pCache->boundTags);
×
1277
    taosMemoryFreeClear(pCache->boundTags);
×
1278

1279
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
1280
  }
1281
  taosHashCleanup(pStmt->sql.pTableCache);
248✔
1282

1283
  if (pStmt->sql.stbInterlaceMode) {
248✔
1284
    pStmt->bInfo.tagsCached = false;
248✔
1285
  }
1286
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
248✔
1287

1288
  resetRequest(pStmt);
248✔
1289

1290
  if (pStmt->sql.siInfo.pTableCols) {
248✔
1291
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
248✔
1292
    pStmt->sql.siInfo.pTableCols = NULL;
248✔
1293
  }
1294

1295
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
248✔
1296
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
248✔
1297
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
248✔
1298
  }
1299

1300
  if (pStmt->sql.siInfo.pTableHash) {
248✔
1301
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
248✔
1302
    pStmt->sql.siInfo.pTableHash = NULL;
248✔
1303
  }
1304

1305
  if (pStmt->sql.siInfo.pTableRowDataHash) {
248✔
1306
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
248✔
1307
    pStmt->sql.siInfo.pTableRowDataHash = NULL;
248✔
1308
  }
1309

1310
  if (pStmt->sql.siInfo.pVgroupHash) {
248✔
1311
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1312
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1313
  }
1314

1315
  if (pStmt->sql.siInfo.pVgroupList) {
248✔
1316
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
1317
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1318
  }
1319

1320
  if (pStmt->sql.siInfo.pDataCtx) {
248✔
1321
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
248✔
1322
    pStmt->sql.siInfo.pDataCtx = NULL;
248✔
1323
  }
1324

1325
  if (pStmt->sql.siInfo.pTSchema) {
248✔
1326
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
248✔
1327
    pStmt->sql.siInfo.pTSchema = NULL;
248✔
1328
  }
1329

1330
  if (pStmt->sql.siInfo.pRequest) {
248✔
1331
    taos_free_result(pStmt->sql.siInfo.pRequest);
248✔
1332
    pStmt->sql.siInfo.pRequest = NULL;
248✔
1333
  }
1334

1335
  if (stbInterlaceMode) {
248✔
1336
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
248✔
1337
  }
1338

1339
  pStmt->db = db;
248✔
1340
  pStmt->stbInterlaceMode = stbInterlaceMode;
248✔
1341
  pStmt->options = options;
248✔
1342
  pStmt->reqid = reqid;
248✔
1343

1344
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
248✔
1345
  if (NULL == pStmt->sql.pTableCache) {
248✔
1346
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
1347
    return terrno;
×
1348
  }
1349

1350
  pStmt->sql.status = STMT_INIT;
248✔
1351

1352
  return TSDB_CODE_SUCCESS;
248✔
1353
}
1354

1355
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
17,379✔
1356
  STscStmt2* pStmt = (STscStmt2*)stmt;
17,379✔
1357
  int32_t    code = 0;
17,379✔
1358

1359
  STMT2_DLOG("start to prepare with sql:%s", sql);
17,379✔
1360

1361
  if (stmt == NULL || sql == NULL) {
17,379✔
1362
    STMT2_ELOG_E("stmt or sql is NULL");
×
1363
    return TSDB_CODE_INVALID_PARA;
×
1364
  }
1365

1366
  if (pStmt->sql.status >= STMT_PREPARE) {
17,379✔
1367
    STMT2_DLOG("stmt status is %d, need to reset stmt2 cache before prepare", pStmt->sql.status);
248✔
1368
    STMT_ERR_RET(stmtDeepReset(pStmt));
248✔
1369
  }
1370

1371
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
17,379✔
1372
    STMT2_ELOG("errCode is not success before, ErrCode: 0x%x, errorsyt: %s\n. ", pStmt->errCode,
×
1373
               tstrerror(pStmt->errCode));
1374
    return pStmt->errCode;
×
1375
  }
1376

1377
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
17,379✔
1378

1379
  if (length <= 0) {
17,379✔
1380
    length = strlen(sql);
29✔
1381
  }
1382
  pStmt->sql.sqlStr = taosStrndup(sql, length);
17,379✔
1383
  if (!pStmt->sql.sqlStr) {
17,379✔
1384
    STMT2_ELOG("fail to allocate memory for sqlStr:%s", tstrerror(terrno));
×
1385
    STMT_ERR_RET(terrno);
×
1386
  }
1387
  pStmt->sql.sqlLen = length;
17,379✔
1388
  STMT_ERR_RET(stmtCreateRequest(pStmt));
17,379✔
1389

1390
  if (stmt2IsInsert(pStmt)) {
17,379✔
1391
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
17,350✔
1392
    char* dbName = NULL;
17,350✔
1393
    if (qParseDbName(sql, length, &dbName)) {
17,350✔
1394
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
1395
      taosMemoryFreeClear(dbName);
×
1396
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
17,099✔
1397
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
17,350✔
1398
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
17,350✔
1399
      if (pStmt->exec.pRequest->pDb == NULL) {
17,350✔
1400
        STMT_ERR_RET(terrno);
×
1401
      }
1402
      (void)strdequote(pStmt->exec.pRequest->pDb);
17,350✔
1403

1404
      if (pStmt->sql.stbInterlaceMode) {
17,099✔
1405
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
17,019✔
1406
      }
1407
    }
1408

1409
  } else if (stmt2IsSelect(pStmt)) {
29✔
1410
    pStmt->sql.stbInterlaceMode = false;
29✔
1411
    STMT_ERR_RET(stmtParseSql(pStmt));
29✔
1412
  } else {
1413
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
×
1414
  }
1415
  return TSDB_CODE_SUCCESS;
17,128✔
1416
}
1417

1418
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
17,019✔
1419
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
17,019✔
1420
  if (!pSrc) {
17,019✔
1421
    return terrno;
×
1422
  }
1423
  STableDataCxt* pDst = NULL;
17,019✔
1424

1425
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
17,019✔
1426
  pStmt->sql.siInfo.pDataCtx = pDst;
17,270✔
1427

1428
  SArray* pTblCols = NULL;
17,270✔
1429
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
17,156,098✔
1430
    pTblCols = taosArrayInit(20, POINTER_BYTES);
17,138,828✔
1431
    if (NULL == pTblCols) {
17,128,014✔
1432
      return terrno;
×
1433
    }
1434

1435
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
34,042,751✔
1436
      return terrno;
×
1437
    }
1438
  }
1439

1440
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
17,270✔
1441

1442
  STMT2_TLOG("init stb interlace table info, tbName:%s, pDataCtx:%p, boundTags:%p", pStmt->bInfo.tbFName,
17,270✔
1443
             pStmt->sql.siInfo.pDataCtx, pStmt->sql.siInfo.boundTags);
1444

1445
  return TSDB_CODE_SUCCESS;
17,270✔
1446
}
1447

1448
bool stmt2IsInsert(TAOS_STMT2* stmt) {
3,666,350✔
1449
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,666,350✔
1450
  if (pStmt->sql.type) {
3,666,350✔
1451
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
3,632,232✔
1452
  }
1453

1454
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
34,620✔
1455
}
1456

1457
bool stmt2IsSelect(TAOS_STMT2* stmt) {
17,302✔
1458
  STscStmt2* pStmt = (STscStmt2*)stmt;
17,302✔
1459

1460
  if (pStmt->sql.type) {
17,302✔
1461
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1462
  }
1463
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
17,047✔
1464
}
1465

1466
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
3,648,096✔
1467
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,648,096✔
1468

1469
  int64_t startUs = taosGetTimestampUs();
3,646,663✔
1470

1471
  STMT2_TLOG("start to set tbName:%s", tbName);
3,646,663✔
1472

1473
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
3,647,004✔
1474
    return pStmt->errCode;
×
1475
  }
1476

1477
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
3,650,020✔
1478

1479
  int32_t insert = 0;
3,647,625✔
1480
  if (!stmt2IsInsert(stmt)) {
3,647,625✔
1481
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
1482
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1483
  }
1484

1485
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
3,648,537✔
1486
    STMT_ERR_RET(stmtCreateRequest(pStmt));
19,497✔
1487

1488
    STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
17,867✔
1489
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1490
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
17,612✔
1491

1492
    STMT_ERR_RET(stmtGetFromCache(pStmt));
17,365✔
1493

1494
    if (pStmt->bInfo.needParse) {
17,110✔
1495
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
17,095✔
1496
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
17,095✔
1497

1498
      STMT_ERR_RET(stmtParseSql(pStmt));
17,350✔
1499
      if (!pStmt->sql.autoCreateTbl) {
17,350✔
1500
        uint64_t uid, suid;
16,685✔
1501
        int32_t  vgId;
16,685✔
1502
        int8_t   tableType;
16,685✔
1503

1504
        int32_t code = stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType);
16,841✔
1505
        if (code != TSDB_CODE_SUCCESS) {
16,590✔
1506
          return code;
×
1507
        }
1508
      }
1509
    }
1510

1511
  } else {
1512
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
3,628,789✔
1513
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
3,629,550✔
1514
    pStmt->exec.pRequest->requestId++;
3,631,056✔
1515
    pStmt->bInfo.needParse = false;
3,630,805✔
1516
  }
1517

1518
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
3,649,174✔
1519
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
17,270✔
1520
  }
1521

1522
  int64_t startUs2 = taosGetTimestampUs();
3,648,948✔
1523
  pStmt->stat.setTbNameUs += startUs2 - startUs;
3,648,948✔
1524

1525
  return TSDB_CODE_SUCCESS;
3,649,952✔
1526
}
1527

1528
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
1,252,854✔
1529
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,252,854✔
1530

1531
  STMT2_TLOG_E("start to set tbTags");
1,252,854✔
1532
  if (qDebugFlag & DEBUG_TRACE) {
1,252,854✔
1533
    (void)stmtPrintBindv(stmt, tags, -1, true);
×
1534
  }
1535

1536
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,252,513✔
1537
    return pStmt->errCode;
×
1538
  }
1539

1540
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
1,252,513✔
1541

1542
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,252,482✔
1543
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1544
    pStmt->bInfo.needParse = false;
×
1545
  }
1546
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,252,482✔
1547

1548
  if (pStmt->bInfo.needParse) {
1,252,792✔
1549
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1550
  }
1551
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,252,792✔
1552
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1553
  }
1554

1555
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
1,252,792✔
1556
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1557
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1558
  //   return TSDB_CODE_SUCCESS;
1559
  // }
1560
  if (pStmt->sql.autoCreateTbl && pStmt->sql.stbInterlaceMode) {
1,252,792✔
1561
    STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,252,164✔
1562
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1563
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,251,823✔
1564
  }
1565

1566
  STableDataCxt** pDataBlock = NULL;
1,252,885✔
1567
  if (pStmt->exec.pCurrBlock) {
1,252,885✔
1568
    pDataBlock = &pStmt->exec.pCurrBlock;
1,252,686✔
1569
  } else {
1570
    pDataBlock =
1571
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
199✔
1572
    if (NULL == pDataBlock) {
199✔
1573
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1574
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1575
    }
1576
  }
1577
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
1,252,885✔
1578
    return TSDB_CODE_SUCCESS;
×
1579
  }
1580

1581
  STMT2_TLOG_E("start to bind stmt tag values");
1,252,885✔
1582

1583
  void* boundTags = NULL;
1,252,792✔
1584
  if (pStmt->sql.stbInterlaceMode) {
1,252,792✔
1585
    boundTags = pStmt->sql.siInfo.boundTags;
1,252,195✔
1586
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
1,252,195✔
1587
    if (NULL == pCreateTbReq) {
1,252,195✔
1588
      return terrno;
×
1589
    }
1590
    int32_t vgId = -1;
1,252,195✔
1591
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
1,252,195✔
1592
    (*pCreateTbReq)->uid = vgId;
1,252,598✔
1593
  } else {
1594
    boundTags = pStmt->bInfo.boundTags;
597✔
1595
  }
1596

1597
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
1,253,195✔
1598
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1599
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1600

1601
  return TSDB_CODE_SUCCESS;
1,252,885✔
1602
}
1603

1604
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
247,752✔
1605
  STscStmt2* pStmt = (STscStmt2*)stmt;
247,752✔
1606

1607
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
247,752✔
1608

1609
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
247,752✔
1610
    return pStmt->errCode;
×
1611
  }
1612

1613
  if (!pStmt->sql.stbInterlaceMode) {
247,752✔
1614
    return TSDB_CODE_SUCCESS;
×
1615
  }
1616

1617
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
247,752✔
1618

1619
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
247,473✔
1620
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1621
    pStmt->bInfo.needParse = false;
×
1622
  }
1623
  STMT_ERR_RET(stmtCreateRequest(pStmt));
247,473✔
1624

1625
  if (pStmt->bInfo.needParse) {
247,752✔
1626
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1627
    if (!pStmt->sql.autoCreateTbl) {
×
1628
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1629
      return TSDB_CODE_SUCCESS;
×
1630
    }
1631
  }
1632

1633
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
247,752✔
1634
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1635
  }
1636

1637
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
247,752✔
1638
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1639
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
247,752✔
1640

1641
  STableDataCxt** pDataBlock = NULL;
247,752✔
1642
  if (pStmt->exec.pCurrBlock) {
247,752✔
1643
    pDataBlock = &pStmt->exec.pCurrBlock;
247,442✔
1644
  } else {
1645
    pDataBlock =
1646
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
310✔
1647
    if (NULL == pDataBlock) {
310✔
1648
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1649
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
186✔
1650
    }
1651
  }
1652

1653
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
247,938✔
1654
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1655
    return TSDB_CODE_SUCCESS;
×
1656
  }
1657

1658
  if (pStmt->sql.fixValueTags) {
247,938✔
1659
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
247,659✔
1660
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
247,659✔
1661
    if ((*pCreateTbReq)->name) {
247,504✔
1662
      taosMemoryFree((*pCreateTbReq)->name);
247,504✔
1663
    }
1664
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
247,504✔
1665
    int32_t vgId = -1;
247,535✔
1666
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
247,535✔
1667
    (*pCreateTbReq)->uid = vgId;
247,690✔
1668
    return TSDB_CODE_SUCCESS;
247,690✔
1669
  }
1670

1671
  if ((*pDataBlock)->pData->pCreateTbReq) {
279✔
1672
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
310✔
1673
    pStmt->sql.fixValueTags = true;
310✔
1674
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
310✔
1675
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
310✔
1676
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
310✔
1677
  }
1678

1679
  return TSDB_CODE_SUCCESS;
279✔
1680
}
1681

1682
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1683
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1684
    return pStmt->errCode;
×
1685
  }
1686

1687
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1688
    tscError("invalid operation to get query column fileds");
×
1689
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1690
  }
1691

1692
  STableDataCxt** pDataBlock = NULL;
×
1693

1694
  if (pStmt->sql.stbInterlaceMode) {
×
1695
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1696
  } else {
1697
    pDataBlock =
1698
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1699
    if (NULL == pDataBlock) {
×
1700
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1701
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1702
    }
1703
  }
1704

1705
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1706

1707
  return TSDB_CODE_SUCCESS;
×
1708
}
1709

1710
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
77✔
1711
  int32_t code = 0;
77✔
1712
  int32_t preCode = pStmt->errCode;
77✔
1713

1714
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
77✔
1715
    return pStmt->errCode;
×
1716
  }
1717

1718
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
77✔
1719
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1720
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1721
  }
1722

1723
  STableDataCxt** pDataBlock = NULL;
77✔
1724
  bool            cleanStb = false;
77✔
1725

1726
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
77✔
1727
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1728
  } else {
1729
    cleanStb = true;
77✔
1730
    pDataBlock =
1731
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
77✔
1732
  }
1733

1734
  if (NULL == pDataBlock || NULL == *pDataBlock) {
77✔
1735
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1736
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1737
  }
1738

1739
  pStmt->sql.placeholderOfTags = 0;
77✔
1740
  pStmt->sql.placeholderOfCols = 0;
77✔
1741
  int32_t totalNum = 0;
77✔
1742
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.boundCols,
77✔
1743
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1744
                                        &pStmt->sql.placeholderOfCols));
1745

1746
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
77✔
1747
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
77✔
1748
    qDestroyStmtDataBlock(*pDataBlock);
77✔
1749
    *pDataBlock = NULL;
77✔
1750
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
77✔
1751
      STMT2_ELOG("fail to remove remove stb:%s exec blockHash", pStmt->bInfo.tbFName);
×
1752
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1753
    }
1754
    pStmt->sql.autoCreateTbl = false;
77✔
1755
    pStmt->bInfo.tagsCached = false;
77✔
1756
    pStmt->bInfo.sname = (SName){0};
77✔
1757
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
77✔
1758
  }
1759

1760
  if (fieldNum != NULL) {
77✔
1761
    *fieldNum = totalNum;
77✔
1762
  }
1763

1764
  STMT2_DLOG("get insert fields totalNum:%d, tagsNum:%d, colsNum:%d", totalNum, pStmt->sql.placeholderOfTags,
77✔
1765
             pStmt->sql.placeholderOfCols);
1766

1767
_return:
77✔
1768

1769
  pStmt->errCode = preCode;
77✔
1770

1771
  return code;
77✔
1772
}
1773
/*
1774
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1775
  while (true) {
1776
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1777
      pStmt->exec.smInfo.pColIdx = 0;
1778
    }
1779

1780
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1781
      taosUsleep(1);
1782
      continue;
1783
    }
1784

1785
    *idx = pStmt->exec.smInfo.pColIdx;
1786
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1787
  }
1788
}
1789
*/
1790
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
3,648,564✔
1791
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
3,648,564✔
1792
    pStmt->sql.siInfo.pVgroupHash =
1,904,426✔
1793
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
1,904,364✔
1794
  }
1795
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
3,650,136✔
1796
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
1,904,426✔
1797
  }
1798

1799
  if (NULL == pStmt->sql.siInfo.pRequest) {
3,650,421✔
1800
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
17,270✔
1801
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1802

1803
    if (pStmt->reqid != 0) {
17,019✔
1804
      pStmt->reqid++;
26✔
1805
    }
1806
    pStmt->exec.pRequest->syncQuery = true;
17,270✔
1807

1808
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
17,019✔
1809
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
17,270✔
1810
  }
1811

1812
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
3,650,170✔
1813
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
111,638✔
1814
    pStmt->sql.siInfo.tbFromHash = true;
10,876✔
1815
  }
1816

1817
  if (0 == pStmt->sql.siInfo.firstName[0]) {
3,650,170✔
1818
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
17,022✔
1819
  }
1820

1821
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
3,649,919✔
1822
  param->next = NULL;
3,650,170✔
1823

1824
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
3,649,417✔
1825

1826
  if (pStmt->queue.stopQueue) {
3,651,044✔
1827
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1828
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1829
  }
1830
  stmtEnqueue(pStmt, param);
3,650,542✔
1831

1832
  return TSDB_CODE_SUCCESS;
3,648,896✔
1833
}
1834

1835
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1836
  while (true) {
1837
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
3,649,254✔
1838
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
3,648,791✔
1839
      break;
3,647,339✔
1840
    } else {
1841
      SArray* pTblCols = NULL;
×
1842
      for (int32_t i = 0; i < 100; i++) {
×
1843
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1844
        if (NULL == pTblCols) {
×
1845
          return terrno;
×
1846
        }
1847

1848
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1849
          return terrno;
×
1850
        }
1851
      }
1852
    }
1853
  }
1854

1855
  return TSDB_CODE_SUCCESS;
3,647,339✔
1856
}
1857

1858
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
597✔
1859
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
597✔
1860
    return TSDB_CODE_SUCCESS;
×
1861
  }
1862

1863
  uint64_t uid = pStmt->bInfo.tbUid;
597✔
1864
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
597✔
1865

1866
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
597✔
1867
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
517✔
1868
    return TSDB_CODE_SUCCESS;
517✔
1869
  }
1870

1871
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
80✔
1872
  if (!pSrc) {
80✔
1873
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1874
    return terrno;
×
1875
  }
1876
  STableDataCxt* pDst = NULL;
80✔
1877

1878
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
80✔
1879

1880
  SStmtTableCache cache = {
80✔
1881
      .pDataCtx = pDst,
1882
      .boundTags = pStmt->bInfo.boundTags,
80✔
1883
  };
1884

1885
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
80✔
1886
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
1887
    return terrno;
×
1888
  }
1889

1890
  if (pStmt->sql.autoCreateTbl) {
80✔
1891
    pStmt->bInfo.tagsCached = true;
80✔
1892
  } else {
1893
    pStmt->bInfo.boundTags = NULL;
×
1894
  }
1895

1896
  return TSDB_CODE_SUCCESS;
80✔
1897
}
1898

1899
static int stmtAddBatch2(TAOS_STMT2* stmt) {
1,904,771✔
1900
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,904,771✔
1901

1902
  int64_t startUs = taosGetTimestampUs();
1,905,212✔
1903

1904
  // STMT2_TLOG_E("start to add batch");
1905

1906
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,905,212✔
1907
    return pStmt->errCode;
×
1908
  }
1909

1910
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
1,904,204✔
1911

1912
  if (pStmt->sql.stbInterlaceMode) {
1,904,362✔
1913
    int64_t startUs2 = taosGetTimestampUs();
1,904,140✔
1914
    pStmt->stat.addBatchUs += startUs2 - startUs;
1,904,140✔
1915

1916
    pStmt->sql.siInfo.tableColsReady = false;
1,903,132✔
1917

1918
    SStmtQNode* param = NULL;
1,903,387✔
1919
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
3,805,171✔
1920
    param->restoreTbCols = true;
1,903,545✔
1921
    param->next = NULL;
1,903,545✔
1922

1923
    if (pStmt->sql.autoCreateTbl) {
1,903,047✔
1924
      pStmt->bInfo.tagsCached = true;
498,523✔
1925
    }
1926
    pStmt->bInfo.boundColsCached = true;
1,901,031✔
1927

1928
    if (pStmt->queue.stopQueue) {
1,903,800✔
1929
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1930
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1931
    }
1932

1933
    stmtEnqueue(pStmt, param);
1,903,545✔
1934

1935
    return TSDB_CODE_SUCCESS;
1,904,959✔
1936
  }
1937

1938
  STMT_ERR_RET(stmtCacheBlock(pStmt));
597✔
1939

1940
  return TSDB_CODE_SUCCESS;
597✔
1941
}
1942
/*
1943
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1944
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1945
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1946
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1947

1948
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1949
  pRes->fields = taosMemoryMalloc(size);
1950
  pRes->userFields = taosMemoryMalloc(size);
1951
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1952
    STMT_ERR_RET(terrno);
1953
  }
1954
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1955
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1956

1957
  return TSDB_CODE_SUCCESS;
1958
}
1959

1960
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1961
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1962
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1963

1964
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1965
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1966

1967
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1968
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1969
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1970
      STMT_ERR_RET(terrno);
1971
    }
1972
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1973
  }
1974

1975
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1976
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1977
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1978
      STMT_ERR_RET(terrno);
1979
    }
1980
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1981
  }
1982

1983
  return TSDB_CODE_SUCCESS;
1984
}
1985
*/
1986

1987
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
3,646,879✔
1988
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,646,879✔
1989
  int32_t    code = 0;
3,646,879✔
1990

1991
  int64_t startUs = taosGetTimestampUs();
3,646,116✔
1992

1993
  if (qDebugFlag & DEBUG_TRACE) {
3,646,116✔
1994
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1995
  }
1996

1997
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
3,646,085✔
1998
    return pStmt->errCode;
×
1999
  }
2000

2001
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
3,649,854✔
2002

2003
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
3,648,976✔
2004
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2005
    pStmt->bInfo.needParse = false;
×
2006
  }
2007

2008
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
3,648,215✔
2009
    resetRequest(pStmt);
×
2010
  }
2011

2012
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3,648,717✔
2013
  if (pStmt->bInfo.needParse) {
3,648,728✔
2014
    code = stmtParseSql(pStmt);
×
2015
    if (code != TSDB_CODE_SUCCESS) {
×
2016
      goto cleanup_root;
×
2017
    }
2018
  }
2019

2020
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
3,647,469✔
2021
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
29✔
2022
    if (code != TSDB_CODE_SUCCESS) {
29✔
2023
      goto cleanup_root;
×
2024
    }
2025
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
29✔
2026
                         .acctId = pStmt->taos->acctId,
29✔
2027
                         .db = pStmt->exec.pRequest->pDb,
29✔
2028
                         .topicQuery = false,
2029
                         .pSql = pStmt->sql.sqlStr,
29✔
2030
                         .sqlLen = pStmt->sql.sqlLen,
29✔
2031
                         .pMsg = pStmt->exec.pRequest->msgBuf,
29✔
2032
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2033
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
29✔
2034
                         .pStmtCb = NULL,
2035
                         .pUser = pStmt->taos->user,
29✔
2036
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
29✔
2037
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
29✔
2038
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
29✔
2039
    if (code != TSDB_CODE_SUCCESS) {
29✔
2040
      goto cleanup_root;
×
2041
    }
2042
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
29✔
2043
    if (code != TSDB_CODE_SUCCESS) {
29✔
2044
      goto cleanup_root;
×
2045
    }
2046

2047
    if (pStmt->sql.pQuery->haveResultSet) {
29✔
2048
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
58✔
2049
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
2050
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
29✔
2051
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
29✔
2052
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
29✔
2053
    }
2054

2055
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
29✔
2056
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
29✔
2057
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
29✔
2058

2059
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
2060
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
2061
    // }
2062

2063
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2064

2065
    return TSDB_CODE_SUCCESS;
29✔
2066

2067
  cleanup_root:
×
2068
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
×
2069
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
×
2070
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
×
2071
      pStmt->sql.pQuery->pRoot = NULL;
×
2072
    }
2073
    STMT_ERR_RET(code);
×
2074
  }
2075

2076
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
3,647,687✔
2077
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2078
  }
2079

2080
  STableDataCxt** pDataBlock = NULL;
3,647,312✔
2081

2082
  if (pStmt->exec.pCurrBlock) {
3,647,312✔
2083
    pDataBlock = &pStmt->exec.pCurrBlock;
3,631,225✔
2084
  } else {
2085
    pDataBlock =
2086
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
17,350✔
2087
    if (NULL == pDataBlock) {
17,350✔
2088
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
2089
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2090
    }
2091
    pStmt->exec.pCurrBlock = *pDataBlock;
17,350✔
2092
    if (pStmt->sql.stbInterlaceMode) {
17,350✔
2093
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
17,270✔
2094
      (*pDataBlock)->pData->aCol = NULL;
17,270✔
2095
    }
2096
    if (colIdx < -1) {
17,350✔
2097
      pStmt->sql.bindRowFormat = true;
×
2098
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
2099
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
2100
    }
2101
  }
2102

2103
  int64_t startUs2 = taosGetTimestampUs();
3,649,608✔
2104
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
3,649,608✔
2105

2106
  SStmtQNode* param = NULL;
3,648,098✔
2107
  if (pStmt->sql.stbInterlaceMode) {
3,648,851✔
2108
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
7,293,363✔
2109
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
7,294,836✔
2110
    taosArrayClear(param->tblData.aCol);
3,647,339✔
2111

2112
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2113

2114
    param->restoreTbCols = false;
3,645,096✔
2115
    param->tblData.isOrdered = true;
3,645,096✔
2116
    param->tblData.isDuplicateTs = false;
3,645,845✔
2117
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
3,645,841✔
2118

2119
    param->pCreateTbReq = pCreateTbReq;
3,648,857✔
2120
  }
2121

2122
  int64_t startUs3 = taosGetTimestampUs();
3,648,293✔
2123
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
3,648,293✔
2124

2125
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
3,649,046✔
2126
  SBlobSet* pBlob = NULL;
3,649,799✔
2127
  if (colIdx < 0) {
3,646,779✔
2128
    if (pStmt->sql.stbInterlaceMode) {
3,648,685✔
2129
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
3,649,162✔
2130
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
5,524,979✔
2131
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
3,649,668✔
2132
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
3,649,919✔
2133
      param->tblData.isOrdered = (*pDataBlock)->ordered;
3,648,970✔
2134
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
3,649,472✔
2135
    } else {
2136
      if (colIdx == -1) {
539✔
2137
        if (pStmt->sql.bindRowFormat) {
597✔
2138
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2139
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2140
        }
2141
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
597✔
2142
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
597✔
2143
      } else {
2144
        code =
2145
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
×
2146
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
2147
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
×
2148
      }
2149
    }
2150

2151
    if (code) {
3,648,061✔
2152
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
×
2153
      STMT_ERR_RET(code);
×
2154
    }
2155
  } else {
2156
    if (pStmt->sql.stbInterlaceMode) {
×
2157
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2158
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2159
    }
2160

2161
    if (pStmt->sql.bindRowFormat) {
×
2162
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2163
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2164
    }
2165

2166
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
2167
      STMT2_ELOG_E("bind column index not in sequence");
×
2168
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2169
    }
2170

2171
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2172

2173
    if (0 == colIdx) {
×
2174
      pStmt->bInfo.sBindRowNum = bind->num;
×
2175
    }
2176

2177
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
2178
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
×
2179
                                    pStmt->taos->optionInfo.charsetCxt);
×
2180
    if (code) {
×
2181
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
2182
      STMT_ERR_RET(code);
×
2183
    }
2184
  }
2185

2186
  int64_t startUs4 = taosGetTimestampUs();
3,649,540✔
2187
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
3,649,540✔
2188

2189
  if (pStmt->stbInterlaceMode) {
3,650,042✔
2190
    if (param) param->tblData.pBlobSet = pBlob;
3,649,067✔
2191
  }
2192

2193
  if (pStmt->sql.stbInterlaceMode) {
3,650,544✔
2194
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
3,648,596✔
2195
  } else {
2196
    STMT_ERR_RET(stmtAddBatch2(pStmt));
1,205✔
2197
  }
2198

2199
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
3,651,355✔
2200
  return TSDB_CODE_SUCCESS;
3,650,857✔
2201
}
2202

2203
/*
2204
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2205
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2206

2207
  int32_t code = 0;
2208
  int32_t finalCode = 0;
2209
  size_t  keyLen = 0;
2210
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2211
  while (pIter) {
2212
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2213
    char*          key = taosHashGetKey(pIter, &keyLen);
2214

2215
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2216
    if (pMeta->uid) {
2217
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2218
      continue;
2219
    }
2220

2221
    SSubmitBlkRsp* blkRsp = NULL;
2222
    int32_t        i = 0;
2223
    for (; i < pRsp->nBlocks; ++i) {
2224
      blkRsp = pRsp->pBlocks + i;
2225
      if (strlen(blkRsp->tblFName) != keyLen) {
2226
        continue;
2227
      }
2228

2229
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2230
        continue;
2231
      }
2232

2233
      break;
2234
    }
2235

2236
    if (i < pRsp->nBlocks) {
2237
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2238
               blkRsp->uid);
2239

2240
      pMeta->uid = blkRsp->uid;
2241
      pStmt->bInfo.tbUid = blkRsp->uid;
2242
    } else {
2243
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2244
      if (NULL == pStmt->pCatalog) {
2245
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2246
        if (code) {
2247
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2248
          finalCode = code;
2249
          continue;
2250
        }
2251
      }
2252

2253
      code = stmtCreateRequest(pStmt);
2254
      if (code) {
2255
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2256
        finalCode = code;
2257
        continue;
2258
      }
2259

2260
      STableMeta*      pTableMeta = NULL;
2261
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2262
                               .requestId = pStmt->exec.pRequest->requestId,
2263
                               .requestObjRefId = pStmt->exec.pRequest->self,
2264
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2265
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2266

2267
      pStmt->stat.ctgGetTbMetaNum++;
2268

2269
      taos_free_result(pStmt->exec.pRequest);
2270
      pStmt->exec.pRequest = NULL;
2271

2272
      if (code || NULL == pTableMeta) {
2273
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2274
        finalCode = code;
2275
        taosMemoryFree(pTableMeta);
2276
        continue;
2277
      }
2278

2279
      pMeta->uid = pTableMeta->uid;
2280
      pStmt->bInfo.tbUid = pTableMeta->uid;
2281
      taosMemoryFree(pTableMeta);
2282
    }
2283

2284
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2285
  }
2286

2287
  return finalCode;
2288
}
2289
*/
2290
/*
2291
int stmtStaticModeExec(TAOS_STMT* stmt) {
2292
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2293
  int32_t     code = 0;
2294
  SSubmitRsp* pRsp = NULL;
2295
  if (pStmt->sql.staticMode) {
2296
    return TSDB_CODE_TSC_STMT_API_ERROR;
2297
  }
2298

2299
  STMT_DLOG_E("start to exec");
2300

2301
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2302

2303
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2304
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2305

2306
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2307

2308
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2309
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2310
    if (code) {
2311
      pStmt->exec.pRequest->code = code;
2312
    } else {
2313
      tFreeSSubmitRsp(pRsp);
2314
      STMT_ERR_RET(stmtResetStmt(pStmt));
2315
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2316
    }
2317
  }
2318

2319
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2320

2321
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2322
  pStmt->affectedRows += pStmt->exec.affectedRows;
2323

2324
_return:
2325

2326
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2327

2328
  tFreeSSubmitRsp(pRsp);
2329

2330
  ++pStmt->sql.runTimes;
2331

2332
  STMT_RET(code);
2333
}
2334
*/
2335

2336
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
2337
  const STscObj* pTscObj = pRequest->pTscObj;
×
2338

2339
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
2340
  if (*pCxt == NULL) {
×
2341
    return terrno;
×
2342
  }
2343

2344
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
2345
                           .requestRid = pRequest->self,
×
2346
                           .acctId = pTscObj->acctId,
×
2347
                           .db = pRequest->pDb,
×
2348
                           .topicQuery = false,
2349
                           .pSql = pRequest->sqlstr,
×
2350
                           .sqlLen = pRequest->sqlLen,
×
2351
                           .pMsg = pRequest->msgBuf,
×
2352
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2353
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
2354
                           .pStmtCb = NULL,
2355
                           .pUser = pTscObj->user,
×
2356
                           .pEffectiveUser = pRequest->effectiveUser,
×
2357
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
2358
                           .enableSysInfo = pTscObj->sysInfo,
×
2359
                           .async = true,
2360
                           .svrVer = pTscObj->sVer,
×
2361
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
2362
                           .allocatorId = pRequest->allocatorRefId,
×
2363
                           .parseSqlFp = clientParseSql,
2364
                           .parseSqlParam = pWrapper};
2365
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
2366
  (*pCxt)->biMode = biMode;
×
2367
  return TSDB_CODE_SUCCESS;
×
2368
}
2369

2370
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
2371
  STscStmt2*        pStmt = userdata;
×
2372
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
2373
  pStmt->asyncResultAvailable = true;
×
2374
  pStmt->exec.pRequest->inCallback = true;
×
2375

2376
  if (code == TSDB_CODE_SUCCESS) {
×
2377
    pStmt->exec.affectedRows = taos_affected_rows(res);
×
2378
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
2379
  }
2380

2381
  if (fp) {
×
2382
    fp(pStmt->options.userdata, res, code);
×
2383
  }
2384

2385
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
2386
    taosUsleep(1);
×
2387
  }
2388
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
2389
  ++pStmt->sql.runTimes;
×
2390
  if (pStmt->exec.pRequest != NULL) {
×
2391
    pStmt->exec.pRequest->inCallback = false;
×
2392
  }
2393

2394
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
2395
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2396
  }
2397
}
×
2398

2399
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
1,903,750✔
2400
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,903,750✔
2401
  int32_t    code = 0;
1,903,750✔
2402
  int64_t    startUs = taosGetTimestampUs();
1,904,117✔
2403

2404
  STMT2_DLOG_E("start to exec");
1,904,117✔
2405

2406
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,904,024✔
2407
    return pStmt->errCode;
×
2408
  }
2409

2410
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
1,904,538✔
2411
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
1,904,597✔
2412
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2413
  }
2414
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
1,904,755✔
2415

2416
  if (pStmt->sql.stbInterlaceMode) {
1,904,624✔
2417
    STMT_ERR_RET(stmtAddBatch2(pStmt));
1,904,773✔
2418
  }
2419

2420
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1,904,308✔
2421

2422
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
1,904,786✔
2423
    if (pStmt->sql.stbInterlaceMode) {
1,904,162✔
2424
      int64_t startTs = taosGetTimestampUs();
1,904,237✔
2425
      // wait for stmt bind thread to finish
2426
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
7,176,143✔
2427
        taosUsleep(1);
5,272,064✔
2428
      }
2429

2430
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,904,268✔
2431
        return pStmt->errCode;
×
2432
      }
2433

2434
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
1,904,422✔
2435
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
1,904,175✔
2436
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
1,904,770✔
2437
      pStmt->sql.siInfo.pVgroupHash = NULL;
1,904,017✔
2438
      pStmt->sql.siInfo.pVgroupList = NULL;
1,904,017✔
2439
    } else {
2440
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
22✔
2441
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
80✔
2442

2443
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
80✔
2444

2445
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
80✔
2446
    }
2447
  }
2448

2449
  pStmt->asyncResultAvailable = false;
1,902,960✔
2450
  SRequestObj*      pRequest = pStmt->exec.pRequest;
1,903,968✔
2451
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
1,904,219✔
2452
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
1,904,721✔
2453
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2454

2455
  if (!fp) {
1,903,940✔
2456
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1,903,940✔
2457

2458
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1,903,658✔
2459
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2460
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2461
      if (code) {
×
2462
        pStmt->exec.pRequest->code = code;
×
2463

2464
      } else {
2465
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2466
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2467
      }
2468
    }
2469

2470
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
1,904,500✔
2471

2472
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1,902,994✔
2473
    if (affected_rows) {
1,904,287✔
2474
      *affected_rows = pStmt->exec.affectedRows;
1,904,008✔
2475
    }
2476
    pStmt->affectedRows += pStmt->exec.affectedRows;
1,902,530✔
2477

2478
    // wait for stmt bind thread to finish
2479
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
1,904,036✔
2480
      taosUsleep(1);
×
2481
    }
2482

2483
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
1,903,788✔
2484

2485
    ++pStmt->sql.runTimes;
1,904,541✔
2486
  } else {
2487
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
2488
    if (pWrapper == NULL) {
×
2489
      code = terrno;
×
2490
    } else {
2491
      pWrapper->pRequest = pRequest;
×
2492
      pRequest->pWrapper = pWrapper;
×
2493
    }
2494
    if (TSDB_CODE_SUCCESS == code) {
×
2495
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
2496
    }
2497
    pRequest->syncQuery = false;
×
2498
    pRequest->body.queryFp = asyncQueryCb;
×
2499
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
2500

2501
    pStmt->execSemWaited = false;
×
2502
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
2503
  }
2504

2505
_return:
1,903,784✔
2506
  if (code) {
1,903,784✔
2507
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2508
  }
2509
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
1,903,341✔
2510

2511
  STMT_RET(code);
1,904,851✔
2512
}
2513

2514
int stmtClose2(TAOS_STMT2* stmt) {
17,131✔
2515
  STscStmt2* pStmt = (STscStmt2*)stmt;
17,131✔
2516

2517
  STMT2_DLOG_E("start to close stmt");
17,131✔
2518
  taosMemoryFreeClear(pStmt->db);
17,131✔
2519

2520
  if (pStmt->bindThreadInUse) {
17,131✔
2521
    // wait for stmt bind thread to finish
2522
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
17,022✔
2523
      taosUsleep(1);
×
2524
    }
2525

2526
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
17,022✔
2527
    pStmt->queue.stopQueue = true;
17,022✔
2528
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
17,022✔
2529
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
17,022✔
2530

2531
    (void)taosThreadJoin(pStmt->bindThread, NULL);
17,022✔
2532
    pStmt->bindThreadInUse = false;
17,022✔
2533

2534
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
17,022✔
2535
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
17,022✔
2536
  }
2537

2538
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
17,131✔
2539
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
17,131✔
2540
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2541
  }
2542
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
17,131✔
2543

2544
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
17,131✔
2545
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
17,131✔
2546

2547
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
17,131✔
2548
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2549
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2550
    }
2551
  }
2552

2553
  STMT2_DLOG("stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
17,131✔
2554
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2555
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2556
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2557
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2558
             pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2559
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2560
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2561
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2562
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2563
  if (pStmt->sql.stbInterlaceMode) {
17,131✔
2564
    pStmt->bInfo.tagsCached = false;
17,022✔
2565
  }
2566
  pStmt->bInfo.boundColsCached = false;
17,131✔
2567

2568
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
17,131✔
2569

2570
  if (pStmt->options.asyncExecFn) {
17,131✔
2571
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
2572
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2573
    }
2574
  }
2575
  taosMemoryFree(stmt);
17,131✔
2576

2577
  return TSDB_CODE_SUCCESS;
17,131✔
2578
}
2579

2580
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
2581
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2582

2583
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
2584
    return (char*)tstrerror(terrno);
×
2585
  }
2586

2587
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2588
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
×
2589
    pStmt->exec.pRequest->code = terrno;
×
2590
  }
2591

2592
  SRequestObj* pRequest = pStmt->exec.pRequest;
×
2593
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
×
2594
    return pRequest->msgBuf;
×
2595
  }
2596
  return (const char*)tstrerror(pRequest->code);
×
2597
}
2598
/*
2599
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2600

2601
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2602
*/
2603

2604
int stmtParseColFields2(TAOS_STMT2* stmt) {
77✔
2605
  int32_t    code = 0;
77✔
2606
  STscStmt2* pStmt = (STscStmt2*)stmt;
77✔
2607
  int32_t    preCode = pStmt->errCode;
77✔
2608

2609
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
77✔
2610
    return pStmt->errCode;
×
2611
  }
2612

2613
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
77✔
2614
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2615
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2616
  }
2617

2618
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
77✔
2619

2620
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
77✔
2621
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2622
    pStmt->bInfo.needParse = false;
×
2623
  }
2624
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
77✔
2625
    pStmt->bInfo.needParse = false;
×
2626
  }
2627

2628
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
77✔
2629

2630
  if (pStmt->bInfo.needParse) {
77✔
2631
    STMT_ERRI_JRET(stmtParseSql(pStmt));
77✔
2632
  }
2633

2634
_return:
77✔
2635
  // compatible with previous versions
2636
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
77✔
2637
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2638
  }
2639

2640
  pStmt->errCode = preCode;
77✔
2641

2642
  return code;
77✔
2643
}
2644

2645
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
77✔
2646
  int32_t code = stmtParseColFields2(stmt);
77✔
2647
  if (code != TSDB_CODE_SUCCESS) {
77✔
2648
    return code;
×
2649
  }
2650

2651
  return stmtFetchStbColFields2(stmt, nums, fields);
77✔
2652
}
2653

2654
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2655
  int32_t    code = 0;
×
2656
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2657
  int32_t    preCode = pStmt->errCode;
×
2658

2659
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2660
    return pStmt->errCode;
×
2661
  }
2662

2663
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2664

2665
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2666
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2667
    pStmt->bInfo.needParse = false;
×
2668
  }
2669

2670
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2671
    resetRequest(pStmt);
×
2672
  }
2673

2674
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2675

2676
  if (pStmt->bInfo.needParse) {
×
2677
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2678
  }
2679

2680
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2681
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2682
  } else {
2683
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2684
  }
2685

2686
  STMT2_DLOG("get param num success, nums:%d", *nums);
×
2687

2688
_return:
×
2689

2690
  pStmt->errCode = preCode;
×
2691

2692
  return code;
×
2693
}
2694

2695
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
29✔
2696
  STscStmt2* pStmt = (STscStmt2*)stmt;
29✔
2697

2698
  STMT2_TLOG_E("start to use result");
29✔
2699

2700
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
29✔
2701
    STMT2_ELOG_E("useResult only for query statement");
×
2702
    return NULL;
×
2703
  }
2704

2705
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
29✔
2706
    STMT2_ELOG_E("use result after callBackFn return");
×
2707
    return NULL;
×
2708
  }
2709

2710
  return pStmt->exec.pRequest;
29✔
2711
}
2712

2713
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2714
  qInfo("async stmt bind thread started");
×
2715

2716
  ThreadArgs* targs = (ThreadArgs*)args;
×
2717
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2718

2719
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2720
  targs->fp(targs->param, NULL, code);
×
2721
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2722
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2723
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2724
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2725
  taosMemoryFree(args);
×
2726

2727
  qInfo("async stmt bind thread stopped");
×
2728

2729
  return code;
×
2730
}
2731

2732
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
×
2733
  if (pStmt == NULL || msg == NULL) {
×
2734
    return;
×
2735
  }
2736

2737
  if (pStmt->exec.pRequest == NULL) {
×
2738
    return;
×
2739
  }
2740

2741
  if (pStmt->exec.pRequest->msgBuf == NULL) {
×
2742
    return;
×
2743
  }
2744

2745
  size_t msgLen = strlen(msg);
×
2746
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
×
2747

2748
  if (msgLen >= bufLen) {
×
2749
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2750
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
2751
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2752
  } else {
2753
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
×
2754
    pStmt->exec.pRequest->msgBufLen = msgLen;
×
2755
  }
2756

2757
  return;
×
2758
}
2759

2760
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
×
2761
  stmtBuildErrorMsg(pStmt, msg);
×
2762
  pStmt->errCode = errorCode;
×
2763

2764
  return errorCode;
×
2765
}
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