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

taosdata / TDengine / #4877

11 Dec 2025 02:43AM UTC coverage: 64.586% (-0.05%) from 64.632%
#4877

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

673 existing lines in 130 files now uncovered.

163673 of 253417 relevant lines covered (64.59%)

105540806.95 hits per line

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

62.84
/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) {
21,765,043✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
21,764,161✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
21,764,923✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
1,390✔
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;
21,764,161✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
21,767,255✔
42
  int i = 0;
21,767,255✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
86,568,420✔
44
    if (pStmt->queue.stopQueue) {
64,824,027✔
45
      return false;
19,409✔
46
    }
47
    if (i < 10) {
64,804,872✔
48
      taosUsleep(1);
61,767,933✔
49
      i++;
61,760,502✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
3,036,939✔
52
      if (pStmt->queue.stopQueue) {
3,041,073✔
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
3,041,073✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
3,037,518✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
3,040,998✔
60
    }
61
  }
62

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

67
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
21,734,980✔
68
  if (pStmt->queue.head == pStmt->queue.tail) {
21,748,848✔
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;
21,748,848✔
76
  pStmt->queue.head->next = node->next;
21,748,848✔
77
  if (pStmt->queue.tail == node) {
21,748,340✔
78
    pStmt->queue.tail = pStmt->queue.head;
10,720,199✔
79
  }
80
  node->next = NULL;
21,748,340✔
81
  *param = node;
21,748,340✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
21,748,594✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
21,749,635✔
85

86
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
21,749,478✔
87

88
  return true;
21,749,838✔
89
}
90

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

97
  param->next = NULL;
21,745,730✔
98

99
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
21,747,000✔
100

101
  pStmt->queue.tail->next = param;
21,749,436✔
102
  pStmt->queue.tail = param;
21,749,436✔
103
  pStmt->stat.bindDataNum++;
21,749,436✔
104

105
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
21,749,436✔
106
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
21,749,443✔
107

108
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
21,747,945✔
109

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

114
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
26,009,035✔
115
  int32_t code = 0;
26,009,035✔
116

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

136
  return code;
26,008,936✔
137
}
138

139
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
54,531,304✔
140
  int32_t code = 0;
54,531,304✔
141

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
54,531,304✔
143
    STMT2_LOG_SEQ(newStatus);
54,535,412✔
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
54,550,300✔
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) {
54,550,808✔
152
    case STMT_PREPARE:
20,268✔
153
      pStmt->errCode = 0;
20,268✔
154
      break;
19,760✔
155
    case STMT_SETTBNAME:
14,928,464✔
156
      if (STMT_STATUS_EQ(INIT)) {
14,928,464✔
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
14,928,718✔
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
14,928,464✔
163
    case STMT_SETTAGS:
11,023,493✔
164
      if (STMT_STATUS_EQ(INIT)) {
11,023,493✔
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
11,023,493✔
168
    case STMT_FETCH_FIELDS:
620✔
169
      if (STMT_STATUS_EQ(INIT)) {
620✔
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
620✔
173
    case STMT_BIND:
14,928,862✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
14,928,862✔
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;
14,928,608✔
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:
6,826,226✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
6,826,226✔
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
6,826,226✔
193
    case STMT_EXECUTE:
6,822,875✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
6,822,875✔
195
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
223✔
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)) {
6,822,144✔
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
202
        }
203
      }
204
      break;
6,822,621✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
54,549,792✔
211

212
  pStmt->sql.status = newStatus;
54,549,792✔
213

214
  return TSDB_CODE_SUCCESS;
54,549,792✔
215
}
216

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

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
20,416✔
221

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

227
  *tbName = pStmt->bInfo.tbName;
19,796✔
228

229
  return TSDB_CODE_SUCCESS;
20,045✔
230
}
231

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

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

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

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

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

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

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

285
  return TSDB_CODE_SUCCESS;
20,665✔
286
}
287

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

291
  pStmt->sql.pVgHash = pVgHash;
20,665✔
292
  pStmt->exec.pBlockHash = pBlockHash;
20,665✔
293

294
  return TSDB_CODE_SUCCESS;
20,665✔
295
}
296

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

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

305
  pStmt->sql.autoCreateTbl = autoCreateTbl;
20,665✔
306

307
  return TSDB_CODE_SUCCESS;
20,665✔
308
}
309

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

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

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

319
  return TSDB_CODE_SUCCESS;
620✔
320
}
321

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

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

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

335
  pStmt->stat.parseSqlNum++;
20,888✔
336

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

340
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
20,888✔
341

342
  pStmt->bInfo.needParse = false;
20,888✔
343

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

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

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

402
  return TSDB_CODE_SUCCESS;
20,665✔
403
}
404

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

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

NEW
435
  if (code != TSDB_CODE_SUCCESS) {
×
NEW
436
    STMT2_ELOG("failed to get param count, code:%d", code);
×
NEW
437
    return code;
×
438
  }
439

NEW
440
  for (int i = 0; i < count; i++) {
×
NEW
441
    int32_t type = bindv[i].buffer_type;
×
NEW
442
    int32_t num = bindv[i].num;
×
NEW
443
    char*   current_buf = (char*)bindv[i].buffer;
×
444

NEW
445
    for (int j = 0; j < num; j++) {
×
NEW
446
      char    buf[256] = {0};
×
NEW
447
      int32_t len = 0;
×
NEW
448
      bool    isNull = (bindv[i].is_null && bindv[i].is_null[j]);
×
449

NEW
450
      if (IS_VAR_DATA_TYPE(type) && bindv[i].length) {
×
NEW
451
        len = bindv[i].length[j];
×
452
      } else {
NEW
453
        len = tDataTypes[type].bytes;
×
454
      }
455

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

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

NEW
514
      if (!isNull && current_buf) {
×
NEW
515
        current_buf += len;
×
516
      }
517
    }
518
  }
519

NEW
520
  return TSDB_CODE_SUCCESS;
×
521
}
522

523
static void resetRequest(STscStmt2* pStmt) {
22,752✔
524
  if (pStmt->exec.pRequest) {
22,752✔
525
    taos_free_result(pStmt->exec.pRequest);
20,268✔
526
    pStmt->exec.pRequest = NULL;
20,268✔
527
  }
528
  pStmt->asyncResultAvailable = false;
22,752✔
529
}
22,752✔
530

531
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
6,861,681✔
532
  pStmt->bInfo.tbUid = 0;
6,861,681✔
533
  pStmt->bInfo.tbVgId = -1;
6,861,681✔
534
  pStmt->bInfo.tbType = 0;
6,861,173✔
535
  pStmt->bInfo.needParse = true;
6,861,427✔
536
  pStmt->bInfo.inExecCache = false;
6,861,427✔
537

538
  pStmt->bInfo.tbName[0] = 0;
6,861,173✔
539
  pStmt->bInfo.tbFName[0] = 0;
6,861,427✔
540
  if (!pStmt->bInfo.tagsCached) {
6,861,173✔
541
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
3,175,435✔
542
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
3,175,181✔
543
  }
544

545
  if (!pStmt->bInfo.boundColsCached) {
6,860,157✔
546
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
37,460✔
547
    pStmt->bInfo.boundCols = NULL;
37,460✔
548
  }
549

550
  if (!pStmt->sql.autoCreateTbl) {
6,861,427✔
551
    pStmt->bInfo.stbFName[0] = 0;
3,172,973✔
552
    pStmt->bInfo.tbSuid = 0;
3,173,227✔
553
  }
554

555
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
6,860,919✔
556
             pStmt->sql.autoCreateTbl);
557

558
  return TSDB_CODE_SUCCESS;
6,859,658✔
559
}
560

561
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
562
  (void)qResetStmtColumns(pTb->aCol, true);
×
563
  taosArrayDestroy(pTb->aCol);
×
564
}
×
565

566
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
6,819,750✔
567
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
6,819,750✔
568
  if (NULL == pTblBuf->pCurBuff) {
6,821,688✔
569
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
1,688✔
570
    return;
×
571
  }
572
  pTblBuf->buffIdx = 1;
6,820,000✔
573
  pTblBuf->buffOffset = sizeof(*pQueue->head);
6,820,000✔
574

575
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
6,819,746✔
576
  pQueue->qRemainNum = 0;
6,819,746✔
577
  pQueue->head->next = NULL;
6,819,746✔
578
}
579

580
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
6,841,015✔
581
  if (pStmt->sql.stbInterlaceMode) {
6,841,015✔
582
    if (deepClean) {
6,839,099✔
583
      taosHashCleanup(pStmt->exec.pBlockHash);
19,409✔
584
      pStmt->exec.pBlockHash = NULL;
19,409✔
585

586
      if (NULL != pStmt->exec.pCurrBlock) {
19,409✔
587
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
19,409✔
588
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
19,409✔
589
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
19,409✔
590
        pStmt->exec.pCurrBlock = NULL;
19,409✔
591
      }
592
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
19,409✔
593
        resetRequest(pStmt);
19,409✔
594
      }
595
    } else {
596
      pStmt->sql.siInfo.pTableColsIdx = 0;
6,819,690✔
597
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
6,820,198✔
598
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
6,820,115✔
599
    }
600
    if (NULL != pStmt->exec.pRequest) {
6,841,782✔
601
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
6,822,373✔
602
    }
603
  } else {
604
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
2,175✔
605
      resetRequest(pStmt);
1,190✔
606
    }
607

608
    size_t keyLen = 0;
1,718✔
609
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1,718✔
610
    while (pIter) {
7,153✔
611
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
5,435✔
612
      char*          key = taosHashGetKey(pIter, &keyLen);
5,435✔
613
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
5,435✔
614

615
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
5,435✔
616
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
636✔
617
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
1,495✔
618

619
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
636✔
620
        continue;
636✔
621
      }
622

623
      qDestroyStmtDataBlock(pBlocks);
4,799✔
624
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
4,799✔
625

626
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
4,799✔
627
    }
628

629
    if (keepTable) {
1,718✔
630
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
859✔
631
                 keepTable, deepClean);
632
      return TSDB_CODE_SUCCESS;
859✔
633
    }
634

635
    taosHashCleanup(pStmt->exec.pBlockHash);
859✔
636
    pStmt->exec.pBlockHash = NULL;
859✔
637

638
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
859✔
639
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
859✔
640
  }
641

642
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
6,842,641✔
643
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
6,840,774✔
644
             keepTable, deepClean);
645

646
  return TSDB_CODE_SUCCESS;
6,840,364✔
647
}
648

649
static void stmtFreeTbBuf(void* buf) {
19,409✔
650
  void* pBuf = *(void**)buf;
19,409✔
651
  taosMemoryFree(pBuf);
19,409✔
652
}
19,409✔
653

654
static void stmtFreeTbCols(void* buf) {
19,409,000✔
655
  SArray* pCols = *(SArray**)buf;
19,409,000✔
656
  taosArrayDestroy(pCols);
19,409,000✔
657
}
19,409,000✔
658

659
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
18,420✔
660
  STMT2_TLOG_E("start to free SQL info");
18,420✔
661

662
  taosMemoryFree(pStmt->sql.pBindInfo);
18,420✔
663
  taosMemoryFree(pStmt->sql.queryRes.fields);
18,420✔
664
  taosMemoryFree(pStmt->sql.queryRes.userFields);
18,420✔
665
  taosMemoryFree(pStmt->sql.sqlStr);
18,420✔
666
  qDestroyQuery(pStmt->sql.pQuery);
18,420✔
667
  taosArrayDestroy(pStmt->sql.nodeList);
18,420✔
668
  taosHashCleanup(pStmt->sql.pVgHash);
18,420✔
669
  pStmt->sql.pVgHash = NULL;
18,420✔
670
  if (pStmt->sql.fixValueTags) {
18,420✔
671
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
462✔
672
  }
673

674
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
18,420✔
675
  while (pIter) {
19,056✔
676
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
636✔
677

678
    qDestroyStmtDataBlock(pCache->pDataCtx);
636✔
679
    qDestroyBoundColInfo(pCache->boundTags);
636✔
680
    taosMemoryFreeClear(pCache->boundTags);
636✔
681

682
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
636✔
683
  }
684
  taosHashCleanup(pStmt->sql.pTableCache);
18,420✔
685
  pStmt->sql.pTableCache = NULL;
18,420✔
686

687
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
18,420✔
688
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
18,420✔
689

690
  taos_free_result(pStmt->sql.siInfo.pRequest);
18,420✔
691
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
18,420✔
692
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
18,420✔
693
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
18,420✔
694
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
18,420✔
695
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
18,420✔
696
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
18,420✔
697
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
18,420✔
698
  pStmt->sql.siInfo.pTableCols = NULL;
18,420✔
699

700
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
18,420✔
701
  pStmt->sql.siInfo.tableColsReady = true;
18,420✔
702

703
  STMT2_TLOG_E("end to free SQL info");
18,420✔
704

705
  return TSDB_CODE_SUCCESS;
18,420✔
706
}
707

708
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
11,018,008✔
709
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
11,018,008✔
710
    return TSDB_CODE_SUCCESS;
×
711
  }
712

713
  SVgroupInfo      vgInfo = {0};
11,018,008✔
714
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
11,018,008✔
715
                           .requestId = pStmt->exec.pRequest->requestId,
11,018,008✔
716
                           .requestObjRefId = pStmt->exec.pRequest->self,
11,018,008✔
717
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
11,018,008✔
718

719
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
11,022,590✔
720
  if (TSDB_CODE_SUCCESS != code) {
11,022,264✔
721
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
722
    return code;
×
723
  }
724

725
  code =
726
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
11,022,264✔
727
  if (TSDB_CODE_SUCCESS != code) {
11,021,682✔
728
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
×
729
    return code;
×
730
  }
731

732
  *vgId = vgInfo.vgId;
11,022,122✔
733

734
  return TSDB_CODE_SUCCESS;
11,022,122✔
735
}
736

737
int32_t stmtGetTableMetaAndValidate(STscStmt2* pStmt, uint64_t* uid, uint64_t* suid, int32_t* vgId, int8_t* tableType) {
16,203✔
738
  STableMeta*      pTableMeta = NULL;
16,203✔
739
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
16,203✔
740
                           .requestId = pStmt->exec.pRequest->requestId,
16,203✔
741
                           .requestObjRefId = pStmt->exec.pRequest->self,
16,203✔
742
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
16,203✔
743
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
16,203✔
744

745
  pStmt->stat.ctgGetTbMetaNum++;
16,203✔
746

747
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
16,203✔
748
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
×
749
    (void)stmtCleanBindInfo(pStmt);
×
750

751
    if (!pStmt->sql.autoCreateTbl) {
×
752
      STMT2_ELOG("table %s does not exist and autoCreateTbl is disabled", pStmt->bInfo.tbFName);
×
753
      STMT_ERR_RET(TSDB_CODE_PAR_TABLE_NOT_EXIST);
×
754
    }
755

756
    STMT_ERR_RET(code);
×
757
  }
758

759
  STMT_ERR_RET(code);
16,203✔
760

761
  *uid = pTableMeta->uid;
16,203✔
762
  *suid = pTableMeta->suid;
16,203✔
763
  *tableType = pTableMeta->tableType;
16,203✔
764
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
16,203✔
765
  *vgId = pTableMeta->vgId;
16,203✔
766

767
  taosMemoryFree(pTableMeta);
16,203✔
768

769
  return TSDB_CODE_SUCCESS;
16,203✔
770
}
771

772
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
4,163✔
773
                                    uint64_t suid, int32_t vgId) {
774
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
4,163✔
775
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
4,163✔
776

777
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
4,163✔
778

779
  return TSDB_CODE_SUCCESS;
4,163✔
780
}
781

782
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
23,954✔
783
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
23,954✔
784
    pStmt->bInfo.needParse = false;
×
785
    pStmt->bInfo.inExecCache = false;
×
786
    return TSDB_CODE_SUCCESS;
×
787
  }
788

789
  pStmt->bInfo.needParse = true;
24,208✔
790
  pStmt->bInfo.inExecCache = false;
24,208✔
791

792
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
24,208✔
793
  if (pCxtInExec) {
24,208✔
794
    pStmt->bInfo.needParse = false;
×
795
    pStmt->bInfo.inExecCache = true;
×
796

797
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
798

799
    if (pStmt->sql.autoCreateTbl) {
×
800
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
801
      return TSDB_CODE_SUCCESS;
×
802
    }
803
  }
804

805
  if (NULL == pStmt->pCatalog) {
24,208✔
806
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
18,197✔
807
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
18,197✔
808
  }
809

810
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
24,208✔
811
    if (pStmt->bInfo.inExecCache) {
20,045✔
812
      pStmt->bInfo.needParse = false;
×
813
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
814
      return TSDB_CODE_SUCCESS;
×
815
    }
816

817
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
20,045✔
818

819
    return TSDB_CODE_SUCCESS;
20,045✔
820
  }
821

822
  if (pStmt->sql.autoCreateTbl) {
4,163✔
823
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
4,163✔
824
    if (pCache) {
4,163✔
825
      pStmt->bInfo.needParse = false;
4,163✔
826
      pStmt->bInfo.tbUid = 0;
4,163✔
827

828
      STableDataCxt* pNewBlock = NULL;
4,163✔
829
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
4,163✔
830

831
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
4,163✔
832
                      POINTER_BYTES)) {
833
        STMT_ERR_RET(terrno);
×
834
      }
835

836
      pStmt->exec.pCurrBlock = pNewBlock;
4,163✔
837

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

840
      return TSDB_CODE_SUCCESS;
4,163✔
841
    }
842

843
    STMT_RET(stmtCleanBindInfo(pStmt));
×
844
  }
845

846
  uint64_t uid, suid;
×
847
  int32_t  vgId;
×
848
  int8_t   tableType;
×
849

850
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
×
851

852
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
853

854
  if (uid == pStmt->bInfo.tbUid) {
×
855
    pStmt->bInfo.needParse = false;
×
856

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

859
    return TSDB_CODE_SUCCESS;
×
860
  }
861

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

868
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
869
    }
870

871
    pStmt->bInfo.needParse = false;
×
872

873
    pStmt->bInfo.tbUid = uid;
×
874
    pStmt->bInfo.tbSuid = suid;
×
875
    pStmt->bInfo.tbType = tableType;
×
876
    pStmt->bInfo.boundTags = pCache->boundTags;
×
877
    pStmt->bInfo.tagsCached = true;
×
878

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

881
    return TSDB_CODE_SUCCESS;
×
882
  }
883

884
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
885
  if (pCache) {
×
886
    pStmt->bInfo.needParse = false;
×
887

888
    pStmt->bInfo.tbUid = uid;
×
889
    pStmt->bInfo.tbSuid = suid;
×
890
    pStmt->bInfo.tbType = tableType;
×
891
    pStmt->bInfo.boundTags = pCache->boundTags;
×
892
    pStmt->bInfo.tagsCached = true;
×
893

894
    STableDataCxt* pNewBlock = NULL;
×
895
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
896

897
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
898
                    POINTER_BYTES)) {
899
      STMT_ERR_RET(terrno);
×
900
    }
901

902
    pStmt->exec.pCurrBlock = pNewBlock;
×
903

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

906
    return TSDB_CODE_SUCCESS;
×
907
  }
908

909
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
910

911
  return TSDB_CODE_SUCCESS;
×
912
}
913

914
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
915
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
916

917
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
918
  if (NULL == pStmt->sql.pTableCache) {
×
919
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
920
    STMT_ERR_RET(terrno);
×
921
  }
922

923
  pStmt->sql.status = STMT_INIT;
×
924

925
  return TSDB_CODE_SUCCESS;
×
926
}
927

928
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
21,747,016✔
929
  SStmtQNode* pParam = (SStmtQNode*)param;
21,747,016✔
930

931
  if (pParam->restoreTbCols) {
21,747,016✔
932
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
21,746,917✔
933
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
14,925,449✔
934
      *p = taosArrayInit(20, POINTER_BYTES);
14,925,449✔
935
      if (*p == NULL) {
14,925,435✔
936
        pStmt->errCode = terrno;
972✔
937
      }
938
    }
939
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
6,821,468✔
940
    STMT2_TLOG_E("restore pTableCols finished");
6,822,307✔
941
  } else {
942
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
14,924,562✔
943
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
944
    // taosMemoryFree(pParam->pTbData);
945
    if (code != TSDB_CODE_SUCCESS) {
14,922,084✔
946
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
947
      pStmt->errCode = code;
×
948
    }
949
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
14,922,084✔
950
  }
951
}
21,749,007✔
952

953
static void* stmtBindThreadFunc(void* param) {
19,409✔
954
  setThreadName("stmt2Bind");
19,409✔
955

956
  STscStmt2* pStmt = (STscStmt2*)param;
19,409✔
957
  STMT2_DLOG_E("stmt2 bind thread started");
19,409✔
958

959
  while (true) {
21,748,636✔
960
    SStmtQNode* asyncParam = NULL;
21,768,045✔
961

962
    if (!stmtDequeue(pStmt, &asyncParam)) {
21,768,045✔
963
      if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
19,409✔
964
        STMT2_DLOG_E("queue is empty and stopQueue is set, thread will exit");
19,409✔
965
        break;
19,409✔
966
      }
967
      continue;
×
968
    }
969

970
    stmtAsyncOutput(pStmt, asyncParam);
21,747,669✔
971
  }
972

973
  STMT2_DLOG_E("stmt2 bind thread stopped");
19,409✔
974
  return NULL;
19,409✔
975
}
976

977
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
19,409✔
978
  TdThreadAttr thAttr;
15,057✔
979
  if (taosThreadAttrInit(&thAttr) != 0) {
19,409✔
980
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
981
  }
982
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
19,409✔
983
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
984
  }
985

986
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
19,409✔
987
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
988
    STMT_ERR_RET(terrno);
×
989
  }
990

991
  pStmt->bindThreadInUse = true;
19,409✔
992

993
  (void)taosThreadAttrDestroy(&thAttr);
19,409✔
994
  return TSDB_CODE_SUCCESS;
19,409✔
995
}
996

997
static int32_t stmtInitQueue(STscStmt2* pStmt) {
19,409✔
998
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
19,409✔
999
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
19,409✔
1000
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
38,818✔
1001
  pStmt->queue.tail = pStmt->queue.head;
19,409✔
1002

1003
  return TSDB_CODE_SUCCESS;
19,409✔
1004
}
1005

1006
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
18,420✔
1007
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
18,420✔
1008
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
18,420✔
1009
  pStmt->asyncBindParam.asyncBindNum = 0;
18,420✔
1010

1011
  return TSDB_CODE_SUCCESS;
18,420✔
1012
}
1013

1014
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
19,409✔
1015
  pTblBuf->buffUnit = sizeof(SStmtQNode);
19,409✔
1016
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
19,409✔
1017
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
19,409✔
1018
  if (NULL == pTblBuf->pBufList) {
19,409✔
1019
    return terrno;
×
1020
  }
1021
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
19,409✔
1022
  if (NULL == buff) {
19,409✔
1023
    return terrno;
×
1024
  }
1025

1026
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
38,818✔
1027
    return terrno;
×
1028
  }
1029

1030
  pTblBuf->pCurBuff = buff;
19,409✔
1031
  pTblBuf->buffIdx = 1;
19,409✔
1032
  pTblBuf->buffOffset = 0;
19,409✔
1033

1034
  return TSDB_CODE_SUCCESS;
19,409✔
1035
}
1036

1037
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
18,420✔
1038
  STscObj*   pObj = (STscObj*)taos;
18,420✔
1039
  STscStmt2* pStmt = NULL;
18,420✔
1040
  int32_t    code = 0;
18,420✔
1041

1042
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
18,420✔
1043
  if (NULL == pStmt) {
18,420✔
1044
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
1045
    return NULL;
×
1046
  }
1047

1048
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
18,420✔
1049
  if (NULL == pStmt->sql.pTableCache) {
18,420✔
1050
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtInit2:%s", tstrerror(terrno));
×
1051
    taosMemoryFree(pStmt);
×
1052
    return NULL;
×
1053
  }
1054

1055
  pStmt->taos = pObj;
18,420✔
1056
  if (taos->db[0] != '\0') {
18,420✔
1057
    pStmt->db = taosStrdup(taos->db);
18,420✔
1058
  }
1059
  pStmt->bInfo.needParse = true;
18,420✔
1060
  pStmt->sql.status = STMT_INIT;
18,420✔
1061
  pStmt->errCode = TSDB_CODE_SUCCESS;
18,420✔
1062

1063
  if (NULL != pOptions) {
18,420✔
1064
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
17,792✔
1065
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
17,792✔
1066
      pStmt->stbInterlaceMode = true;
17,561✔
1067
    }
1068

1069
    pStmt->reqid = pOptions->reqid;
17,792✔
1070
  }
1071

1072
  if (pStmt->stbInterlaceMode) {
18,420✔
1073
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
17,561✔
1074
    pStmt->sql.siInfo.acctId = taos->acctId;
17,561✔
1075
    pStmt->sql.siInfo.dbname = taos->db;
17,561✔
1076
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
17,561✔
1077

1078
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
17,561✔
1079
    if (NULL == pStmt->sql.siInfo.pTableHash) {
17,561✔
1080
      STMT2_ELOG("fail to allocate memory for pTableHash:%s", tstrerror(terrno));
×
1081
      (void)stmtClose2(pStmt);
×
1082
      return NULL;
×
1083
    }
1084

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

1092
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
17,561✔
1093
    if (NULL == pStmt->sql.siInfo.pTableCols) {
17,561✔
1094
      STMT2_ELOG("fail to allocate memory for pTableCols:%s", tstrerror(terrno));
×
1095
      (void)stmtClose2(pStmt);
×
1096
      return NULL;
×
1097
    }
1098

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

1114
  pStmt->sql.siInfo.tableColsReady = true;
18,420✔
1115
  if (pStmt->options.asyncExecFn) {
18,420✔
1116
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
×
1117
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1118
      STMT2_ELOG("fail to init asyncExecSem:%s", tstrerror(terrno));
×
1119
      (void)stmtClose2(pStmt);
×
1120
      return NULL;
×
1121
    }
1122
  }
1123
  code = stmtIniAsyncBind(pStmt);
18,420✔
1124
  if (TSDB_CODE_SUCCESS != code) {
18,420✔
1125
    terrno = code;
×
1126
    STMT2_ELOG("fail to start init asyncExecSem:%s", tstrerror(code));
×
1127

1128
    (void)stmtClose2(pStmt);
×
1129
    return NULL;
×
1130
  }
1131

1132
  pStmt->execSemWaited = false;
18,420✔
1133

1134
  // STMT_LOG_SEQ(STMT_INIT);
1135

1136
  STMT2_DLOG("stmt2 initialize finished, seqId:%d, db:%s, interlaceMode:%d, asyncExec:%d", pStmt->seqId, pStmt->db,
18,420✔
1137
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1138

1139
  return pStmt;
18,420✔
1140
}
1141

1142
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
1143
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1144
  if (dbName == NULL || dbName[0] == '\0') {
×
1145
    STMT2_ELOG_E("dbname in sql is illegal");
×
1146
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
1147
  }
1148

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

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

1173
  if (pStmt->bindThreadInUse) {
1,848✔
1174
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
1,848✔
1175
      taosUsleep(1);
×
1176
    }
1177
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,848✔
1178
    pStmt->queue.stopQueue = true;
1,848✔
1179
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,848✔
1180
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,848✔
1181

1182
    (void)taosThreadJoin(pStmt->bindThread, NULL);
1,848✔
1183
    pStmt->bindThreadInUse = false;
1,848✔
1184
    pStmt->queue.head = NULL;
1,848✔
1185
    pStmt->queue.tail = NULL;
1,848✔
1186
    pStmt->queue.qRemainNum = 0;
1,848✔
1187

1188
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
1,848✔
1189
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
1,848✔
1190
  }
1191

1192
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
1,848✔
1193
  if (NULL == pStmt->sql.siInfo.pTableHash) {
1,848✔
1194
    return terrno;
×
1195
  }
1196

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

1202
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
1,848✔
1203
  if (NULL == pStmt->sql.siInfo.pTableCols) {
1,848✔
1204
    return terrno;
×
1205
  }
1206

1207
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
1,848✔
1208

1209
  if (TSDB_CODE_SUCCESS == code) {
1,848✔
1210
    code = stmtInitQueue(pStmt);
1,848✔
1211
    pStmt->queue.stopQueue = false;
1,848✔
1212
  }
1213
  if (TSDB_CODE_SUCCESS == code) {
1,848✔
1214
    code = stmtStartBindThread(pStmt);
1,848✔
1215
  }
1216
  if (TSDB_CODE_SUCCESS != code) {
1,848✔
1217
    return code;
×
1218
  }
1219

1220
  return TSDB_CODE_SUCCESS;
1,848✔
1221
}
1222

1223
static int32_t stmtDeepReset(STscStmt2* pStmt) {
1,848✔
1224
  char*             db = pStmt->db;
1,848✔
1225
  bool              stbInterlaceMode = pStmt->stbInterlaceMode;
1,848✔
1226
  TAOS_STMT2_OPTION options = pStmt->options;
1,848✔
1227
  uint32_t          reqid = pStmt->reqid;
1,848✔
1228

1229
  pStmt->errCode = 0;
1,848✔
1230
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
1,848✔
1231
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1232
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1233
    }
1234
    pStmt->execSemWaited = true;
×
1235
  }
1236
  pStmt->sql.autoCreateTbl = false;
1,848✔
1237
  taosMemoryFree(pStmt->sql.pBindInfo);
1,848✔
1238
  pStmt->sql.pBindInfo = NULL;
1,848✔
1239

1240
  taosMemoryFree(pStmt->sql.queryRes.fields);
1,848✔
1241
  pStmt->sql.queryRes.fields = NULL;
1,848✔
1242

1243
  taosMemoryFree(pStmt->sql.queryRes.userFields);
1,848✔
1244
  pStmt->sql.queryRes.userFields = NULL;
1,848✔
1245

1246
  pStmt->sql.type = 0;
1,848✔
1247
  pStmt->sql.runTimes = 0;
1,848✔
1248
  taosMemoryFree(pStmt->sql.sqlStr);
1,848✔
1249
  pStmt->sql.sqlStr = NULL;
1,848✔
1250

1251
  qDestroyQuery(pStmt->sql.pQuery);
1,848✔
1252
  pStmt->sql.pQuery = NULL;
1,848✔
1253

1254
  taosArrayDestroy(pStmt->sql.nodeList);
1,848✔
1255
  pStmt->sql.nodeList = NULL;
1,848✔
1256

1257
  taosHashCleanup(pStmt->sql.pVgHash);
1,848✔
1258
  pStmt->sql.pVgHash = NULL;
1,848✔
1259

1260
  if (pStmt->sql.fixValueTags) {
1,848✔
1261
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
1,848✔
1262
    pStmt->sql.fixValueTbReq = NULL;
1,848✔
1263
  }
1264
  pStmt->sql.fixValueTags = false;
1,848✔
1265

1266
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
1,848✔
1267
  while (pIter) {
1,848✔
1268
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
1269

1270
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
1271
    qDestroyBoundColInfo(pCache->boundTags);
×
1272
    taosMemoryFreeClear(pCache->boundTags);
×
1273

1274
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
1275
  }
1276
  taosHashCleanup(pStmt->sql.pTableCache);
1,848✔
1277

1278
  if (pStmt->sql.stbInterlaceMode) {
1,848✔
1279
    pStmt->bInfo.tagsCached = false;
1,848✔
1280
  }
1281
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
1,848✔
1282

1283
  resetRequest(pStmt);
1,848✔
1284

1285
  if (pStmt->sql.siInfo.pTableCols) {
1,848✔
1286
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
1,848✔
1287
    pStmt->sql.siInfo.pTableCols = NULL;
1,848✔
1288
  }
1289

1290
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
1,848✔
1291
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
1,848✔
1292
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
1,848✔
1293
  }
1294

1295
  if (pStmt->sql.siInfo.pTableHash) {
1,848✔
1296
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
1,848✔
1297
    pStmt->sql.siInfo.pTableHash = NULL;
1,848✔
1298
  }
1299

1300
  if (pStmt->sql.siInfo.pTableRowDataHash) {
1,848✔
1301
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
1,848✔
1302
    pStmt->sql.siInfo.pTableRowDataHash = NULL;
1,848✔
1303
  }
1304

1305
  if (pStmt->sql.siInfo.pVgroupHash) {
1,848✔
1306
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1307
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1308
  }
1309

1310
  if (pStmt->sql.siInfo.pVgroupList) {
1,848✔
1311
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
1312
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1313
  }
1314

1315
  if (pStmt->sql.siInfo.pDataCtx) {
1,848✔
1316
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
1,848✔
1317
    pStmt->sql.siInfo.pDataCtx = NULL;
1,848✔
1318
  }
1319

1320
  if (pStmt->sql.siInfo.pTSchema) {
1,848✔
1321
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
1,848✔
1322
    pStmt->sql.siInfo.pTSchema = NULL;
1,848✔
1323
  }
1324

1325
  if (pStmt->sql.siInfo.pRequest) {
1,848✔
1326
    taos_free_result(pStmt->sql.siInfo.pRequest);
1,848✔
1327
    pStmt->sql.siInfo.pRequest = NULL;
1,848✔
1328
  }
1329

1330
  if (stbInterlaceMode) {
1,848✔
1331
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
1,848✔
1332
  }
1333

1334
  pStmt->db = db;
1,848✔
1335
  pStmt->stbInterlaceMode = stbInterlaceMode;
1,848✔
1336
  pStmt->options = options;
1,848✔
1337
  pStmt->reqid = reqid;
1,848✔
1338

1339
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1,848✔
1340
  if (NULL == pStmt->sql.pTableCache) {
1,848✔
1341
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
1342
    return terrno;
×
1343
  }
1344

1345
  pStmt->sql.status = STMT_INIT;
1,848✔
1346

1347
  return TSDB_CODE_SUCCESS;
1,848✔
1348
}
1349

1350
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
20,268✔
1351
  STscStmt2* pStmt = (STscStmt2*)stmt;
20,268✔
1352
  int32_t    code = 0;
20,268✔
1353

1354
  STMT2_DLOG("start to prepare with sql:%s", sql);
20,268✔
1355

1356
  if (stmt == NULL || sql == NULL) {
20,268✔
1357
    STMT2_ELOG_E("stmt or sql is NULL");
×
1358
    return TSDB_CODE_INVALID_PARA;
×
1359
  }
1360

1361
  if (pStmt->sql.status >= STMT_PREPARE) {
20,268✔
1362
    STMT2_DLOG("stmt status is %d, need to reset stmt2 cache before prepare", pStmt->sql.status);
1,848✔
1363
    STMT_ERR_RET(stmtDeepReset(pStmt));
1,848✔
1364
  }
1365

1366
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,268✔
1367
    STMT2_ELOG("errCode is not success before, ErrCode: 0x%x, errorsyt: %s\n. ", pStmt->errCode,
×
1368
               tstrerror(pStmt->errCode));
1369
    return pStmt->errCode;
×
1370
  }
1371

1372
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
20,268✔
1373

1374
  if (length <= 0) {
20,268✔
1375
    length = strlen(sql);
231✔
1376
  }
1377
  pStmt->sql.sqlStr = taosStrndup(sql, length);
20,268✔
1378
  if (!pStmt->sql.sqlStr) {
20,268✔
1379
    STMT2_ELOG("fail to allocate memory for sqlStr:%s", tstrerror(terrno));
×
1380
    STMT_ERR_RET(terrno);
×
1381
  }
1382
  pStmt->sql.sqlLen = length;
20,268✔
1383
  STMT_ERR_RET(stmtCreateRequest(pStmt));
20,268✔
1384

1385
  if (stmt2IsInsert(pStmt)) {
20,014✔
1386
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
20,045✔
1387
    char* dbName = NULL;
20,045✔
1388
    if (qParseDbName(sql, length, &dbName)) {
20,045✔
1389
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
1390
      taosMemoryFreeClear(dbName);
×
1391
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
19,791✔
1392
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
19,791✔
1393
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
20,299✔
1394
      if (pStmt->exec.pRequest->pDb == NULL) {
20,045✔
1395
        STMT_ERR_RET(terrno);
×
1396
      }
1397
      (void)strdequote(pStmt->exec.pRequest->pDb);
19,791✔
1398

1399
      if (pStmt->sql.stbInterlaceMode) {
20,045✔
1400
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
19,155✔
1401
      }
1402
    }
1403

1404
  } else if (stmt2IsSelect(pStmt)) {
223✔
1405
    pStmt->sql.stbInterlaceMode = false;
223✔
1406
    STMT_ERR_RET(stmtParseSql(pStmt));
223✔
1407
  } else {
1408
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
×
1409
  }
1410
  return TSDB_CODE_SUCCESS;
20,268✔
1411
}
1412

1413
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
19,409✔
1414
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
19,409✔
1415
  if (!pSrc) {
19,409✔
1416
    return terrno;
×
1417
  }
1418
  STableDataCxt* pDst = NULL;
19,409✔
1419

1420
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
19,409✔
1421
  pStmt->sql.siInfo.pDataCtx = pDst;
19,409✔
1422

1423
  SArray* pTblCols = NULL;
19,409✔
1424
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
19,377,486✔
1425
    pTblCols = taosArrayInit(20, POINTER_BYTES);
19,358,077✔
1426
    if (NULL == pTblCols) {
19,316,421✔
1427
      return terrno;
×
1428
    }
1429

1430
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
38,672,630✔
1431
      return terrno;
×
1432
    }
1433
  }
1434

1435
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
19,409✔
1436

1437
  STMT2_TLOG("init stb interlace table info, tbName:%s, pDataCtx:%p, boundTags:%p", pStmt->bInfo.tbFName,
19,409✔
1438
             pStmt->sql.siInfo.pDataCtx, pStmt->sql.siInfo.boundTags);
1439

1440
  return TSDB_CODE_SUCCESS;
19,409✔
1441
}
1442

1443
bool stmt2IsInsert(TAOS_STMT2* stmt) {
14,948,091✔
1444
  STscStmt2* pStmt = (STscStmt2*)stmt;
14,948,091✔
1445
  if (pStmt->sql.type) {
14,948,091✔
1446
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
14,908,225✔
1447
  }
1448

1449
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
39,358✔
1450
}
1451

1452
bool stmt2IsSelect(TAOS_STMT2* stmt) {
19,648✔
1453
  STscStmt2* pStmt = (STscStmt2*)stmt;
19,648✔
1454

1455
  if (pStmt->sql.type) {
19,648✔
1456
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1457
  }
1458
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
19,648✔
1459
}
1460

1461
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
14,927,864✔
1462
  STscStmt2* pStmt = (STscStmt2*)stmt;
14,927,864✔
1463

1464
  int64_t startUs = taosGetTimestampUs();
14,924,929✔
1465

1466
  STMT2_TLOG("start to set tbName:%s", tbName);
14,924,929✔
1467

1468
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
14,926,714✔
1469
    return pStmt->errCode;
×
1470
  }
1471

1472
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
14,928,746✔
1473

1474
  int32_t insert = 0;
14,927,193✔
1475
  if (!stmt2IsInsert(stmt)) {
14,927,193✔
1476
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
1477
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1478
  }
1479

1480
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
14,928,586✔
1481
    STMT_ERR_RET(stmtCreateRequest(pStmt));
28,060✔
1482

1483
    STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
24,208✔
1484
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1485
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
24,208✔
1486

1487
    STMT_ERR_RET(stmtGetFromCache(pStmt));
24,208✔
1488

1489
    if (pStmt->bInfo.needParse) {
24,208✔
1490
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
20,045✔
1491
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
20,045✔
1492

1493
      STMT_ERR_RET(stmtParseSql(pStmt));
20,045✔
1494
      if (!pStmt->sql.autoCreateTbl) {
20,045✔
1495
        uint64_t uid, suid;
15,057✔
1496
        int32_t  vgId;
15,057✔
1497
        int8_t   tableType;
15,057✔
1498

1499
        int32_t code = stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType);
16,203✔
1500
        if (code != TSDB_CODE_SUCCESS) {
16,203✔
1501
          return code;
×
1502
        }
1503
      }
1504
    }
1505

1506
  } else {
1507
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
14,900,526✔
1508
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
14,900,018✔
1509
    pStmt->exec.pRequest->requestId++;
14,900,272✔
1510
    pStmt->bInfo.needParse = false;
14,900,526✔
1511
  }
1512

1513
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
14,924,988✔
1514
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
19,409✔
1515
  }
1516

1517
  int64_t startUs2 = taosGetTimestampUs();
14,929,156✔
1518
  pStmt->stat.setTbNameUs += startUs2 - startUs;
14,929,156✔
1519

1520
  return TSDB_CODE_SUCCESS;
14,928,902✔
1521
}
1522

1523
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
9,174,542✔
1524
  STscStmt2* pStmt = (STscStmt2*)stmt;
9,174,542✔
1525

1526
  STMT2_TLOG_E("start to set tbTags");
9,174,542✔
1527
  if (qDebugFlag & DEBUG_TRACE) {
9,174,542✔
NEW
1528
    (void)stmtPrintBindv(stmt, tags, -1, true);
×
1529
  }
1530

1531
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9,174,698✔
1532
    return pStmt->errCode;
×
1533
  }
1534

1535
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
9,174,698✔
1536

1537
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9,175,039✔
1538
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1539
    pStmt->bInfo.needParse = false;
×
1540
  }
1541
  STMT_ERR_RET(stmtCreateRequest(pStmt));
9,175,039✔
1542

1543
  if (pStmt->bInfo.needParse) {
9,174,443✔
1544
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1545
  }
1546
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
9,174,443✔
1547
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1548
  }
1549

1550
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
9,174,443✔
1551
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1552
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1553
  //   return TSDB_CODE_SUCCESS;
1554
  // }
1555
  if (pStmt->sql.autoCreateTbl && pStmt->sql.stbInterlaceMode) {
9,174,443✔
1556
    STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
9,170,155✔
1557
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1558
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
9,170,779✔
1559
  }
1560

1561
  STableDataCxt** pDataBlock = NULL;
9,175,095✔
1562
  if (pStmt->exec.pCurrBlock) {
9,175,095✔
1563
    pDataBlock = &pStmt->exec.pCurrBlock;
9,173,563✔
1564
  } else {
1565
    pDataBlock =
1566
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1,532✔
1567
    if (NULL == pDataBlock) {
1,532✔
1568
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1569
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1570
    }
1571
  }
1572
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
9,175,095✔
1573
    return TSDB_CODE_SUCCESS;
×
1574
  }
1575

1576
  STMT2_TLOG_E("start to bind stmt tag values");
9,175,095✔
1577

1578
  void* boundTags = NULL;
9,174,201✔
1579
  if (pStmt->sql.stbInterlaceMode) {
9,174,201✔
1580
    boundTags = pStmt->sql.siInfo.boundTags;
9,169,402✔
1581
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
9,169,402✔
1582
    if (NULL == pCreateTbReq) {
9,168,593✔
1583
      return terrno;
×
1584
    }
1585
    int32_t vgId = -1;
9,168,593✔
1586
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
9,168,593✔
1587
    (*pCreateTbReq)->uid = vgId;
9,172,354✔
1588
  } else {
1589
    boundTags = pStmt->bInfo.boundTags;
4,799✔
1590
  }
1591

1592
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
9,177,153✔
1593
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1594
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1595

1596
  return TSDB_CODE_SUCCESS;
9,175,280✔
1597
}
1598

1599
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
1,847,307✔
1600
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,847,307✔
1601

1602
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
1,847,307✔
1603

1604
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,847,694✔
1605
    return pStmt->errCode;
×
1606
  }
1607

1608
  if (!pStmt->sql.stbInterlaceMode) {
1,847,694✔
1609
    return TSDB_CODE_SUCCESS;
×
1610
  }
1611

1612
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
1,847,694✔
1613

1614
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,847,844✔
1615
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1616
    pStmt->bInfo.needParse = false;
×
1617
  }
1618
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,847,844✔
1619

1620
  if (pStmt->bInfo.needParse) {
1,847,457✔
1621
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1622
    if (!pStmt->sql.autoCreateTbl) {
×
1623
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1624
      return TSDB_CODE_SUCCESS;
×
1625
    }
1626
  }
1627

1628
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,847,457✔
1629
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1630
  }
1631

1632
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,847,457✔
1633
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1634
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,847,538✔
1635

1636
  STableDataCxt** pDataBlock = NULL;
1,847,925✔
1637
  if (pStmt->exec.pCurrBlock) {
1,847,925✔
1638
    pDataBlock = &pStmt->exec.pCurrBlock;
1,845,615✔
1639
  } else {
1640
    pDataBlock =
1641
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,310✔
1642
    if (NULL == pDataBlock) {
2,310✔
1643
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
UNCOV
1644
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1645
    }
1646
  }
1647

1648
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
1,847,769✔
1649
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1650
    return TSDB_CODE_SUCCESS;
×
1651
  }
1652

1653
  if (pStmt->sql.fixValueTags) {
1,847,769✔
1654
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
1,845,459✔
1655
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
1,845,459✔
1656
    if ((*pCreateTbReq)->name) {
1,845,147✔
1657
      taosMemoryFree((*pCreateTbReq)->name);
1,845,459✔
1658
    }
1659
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
1,844,910✔
1660
    int32_t vgId = -1;
1,845,384✔
1661
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
1,845,384✔
1662
    (*pCreateTbReq)->uid = vgId;
1,845,690✔
1663
    return TSDB_CODE_SUCCESS;
1,845,690✔
1664
  }
1665

1666
  if ((*pDataBlock)->pData->pCreateTbReq) {
2,310✔
1667
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
2,310✔
1668
    pStmt->sql.fixValueTags = true;
2,310✔
1669
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
2,310✔
1670
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
2,310✔
1671
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
2,310✔
1672
  }
1673

1674
  return TSDB_CODE_SUCCESS;
2,310✔
1675
}
1676

1677
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1678
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1679
    return pStmt->errCode;
×
1680
  }
1681

1682
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1683
    tscError("invalid operation to get query column fileds");
×
1684
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1685
  }
1686

1687
  STableDataCxt** pDataBlock = NULL;
×
1688

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

1700
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1701

1702
  return TSDB_CODE_SUCCESS;
×
1703
}
1704

1705
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
620✔
1706
  int32_t code = 0;
620✔
1707
  int32_t preCode = pStmt->errCode;
620✔
1708

1709
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
620✔
1710
    return pStmt->errCode;
×
1711
  }
1712

1713
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
620✔
1714
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1715
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1716
  }
1717

1718
  STableDataCxt** pDataBlock = NULL;
620✔
1719
  bool            cleanStb = false;
620✔
1720

1721
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
620✔
1722
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1723
  } else {
1724
    cleanStb = true;
620✔
1725
    pDataBlock =
1726
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
620✔
1727
  }
1728

1729
  if (NULL == pDataBlock || NULL == *pDataBlock) {
620✔
1730
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1731
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1732
  }
1733

1734
  pStmt->sql.placeholderOfTags = 0;
620✔
1735
  pStmt->sql.placeholderOfCols = 0;
620✔
1736
  int32_t totalNum = 0;
620✔
1737
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.boundCols,
620✔
1738
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1739
                                        &pStmt->sql.placeholderOfCols));
1740

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

1755
  if (fieldNum != NULL) {
620✔
1756
    *fieldNum = totalNum;
620✔
1757
  }
1758

1759
  STMT2_DLOG("get insert fields totalNum:%d, tagsNum:%d, colsNum:%d", totalNum, pStmt->sql.placeholderOfTags,
620✔
1760
             pStmt->sql.placeholderOfCols);
1761

1762
_return:
620✔
1763

1764
  pStmt->errCode = preCode;
620✔
1765

1766
  return code;
620✔
1767
}
1768
/*
1769
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1770
  while (true) {
1771
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1772
      pStmt->exec.smInfo.pColIdx = 0;
1773
    }
1774

1775
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1776
      taosUsleep(1);
1777
      continue;
1778
    }
1779

1780
    *idx = pStmt->exec.smInfo.pColIdx;
1781
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1782
  }
1783
}
1784
*/
1785
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
14,923,596✔
1786
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
14,923,596✔
1787
    pStmt->sql.siInfo.pVgroupHash =
6,822,075✔
1788
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
6,821,252✔
1789
  }
1790
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
14,924,673✔
1791
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
6,822,075✔
1792
  }
1793

1794
  if (NULL == pStmt->sql.siInfo.pRequest) {
14,924,060✔
1795
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
19,409✔
1796
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1797

1798
    if (pStmt->reqid != 0) {
19,409✔
1799
      pStmt->reqid++;
215✔
1800
    }
1801
    pStmt->exec.pRequest->syncQuery = true;
19,409✔
1802

1803
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
19,409✔
1804
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
19,409✔
1805
  }
1806

1807
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
14,924,060✔
1808
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
119,997✔
1809
    pStmt->sql.siInfo.tbFromHash = true;
11,010✔
1810
  }
1811

1812
  if (0 == pStmt->sql.siInfo.firstName[0]) {
14,924,060✔
1813
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
17,561✔
1814
  }
1815

1816
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
14,924,060✔
1817
  param->next = NULL;
14,924,060✔
1818

1819
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
14,924,060✔
1820

1821
  if (pStmt->queue.stopQueue) {
14,928,018✔
1822
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1823
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1824
  }
1825
  stmtEnqueue(pStmt, param);
14,928,018✔
1826

1827
  return TSDB_CODE_SUCCESS;
14,924,892✔
1828
}
1829

1830
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1831
  while (true) {
1832
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
14,924,795✔
1833
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
14,922,320✔
1834
      break;
14,924,600✔
1835
    } else {
1836
      SArray* pTblCols = NULL;
×
1837
      for (int32_t i = 0; i < 100; i++) {
×
1838
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1839
        if (NULL == pTblCols) {
×
1840
          return terrno;
×
1841
        }
1842

1843
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1844
          return terrno;
×
1845
        }
1846
      }
1847
    }
1848
  }
1849

1850
  return TSDB_CODE_SUCCESS;
14,924,600✔
1851
}
1852

1853
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
4,799✔
1854
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
4,799✔
1855
    return TSDB_CODE_SUCCESS;
×
1856
  }
1857

1858
  uint64_t uid = pStmt->bInfo.tbUid;
4,799✔
1859
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
4,799✔
1860

1861
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
4,799✔
1862
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
4,163✔
1863
    return TSDB_CODE_SUCCESS;
4,163✔
1864
  }
1865

1866
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
636✔
1867
  if (!pSrc) {
636✔
1868
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1869
    return terrno;
×
1870
  }
1871
  STableDataCxt* pDst = NULL;
636✔
1872

1873
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
636✔
1874

1875
  SStmtTableCache cache = {
636✔
1876
      .pDataCtx = pDst,
1877
      .boundTags = pStmt->bInfo.boundTags,
636✔
1878
  };
1879

1880
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
636✔
1881
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
1882
    return terrno;
×
1883
  }
1884

1885
  if (pStmt->sql.autoCreateTbl) {
636✔
1886
    pStmt->bInfo.tagsCached = true;
636✔
1887
  } else {
1888
    pStmt->bInfo.boundTags = NULL;
×
1889
  }
1890

1891
  return TSDB_CODE_SUCCESS;
636✔
1892
}
1893

1894
static int stmtAddBatch2(TAOS_STMT2* stmt) {
6,826,237✔
1895
  STscStmt2* pStmt = (STscStmt2*)stmt;
6,826,237✔
1896

1897
  int64_t startUs = taosGetTimestampUs();
6,826,785✔
1898

1899
  // STMT2_TLOG_E("start to add batch");
1900

1901
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6,826,785✔
1902
    return pStmt->errCode;
×
1903
  }
1904

1905
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
6,826,785✔
1906

1907
  if (pStmt->sql.stbInterlaceMode) {
6,825,900✔
1908
    int64_t startUs2 = taosGetTimestampUs();
6,821,837✔
1909
    pStmt->stat.addBatchUs += startUs2 - startUs;
6,821,837✔
1910

1911
    pStmt->sql.siInfo.tableColsReady = false;
6,821,837✔
1912

1913
    SStmtQNode* param = NULL;
6,821,837✔
1914
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
13,643,064✔
1915
    param->restoreTbCols = true;
6,821,227✔
1916
    param->next = NULL;
6,821,227✔
1917

1918
    if (pStmt->sql.autoCreateTbl) {
6,821,481✔
1919
      pStmt->bInfo.tagsCached = true;
3,682,441✔
1920
    }
1921
    pStmt->bInfo.boundColsCached = true;
6,821,227✔
1922

1923
    if (pStmt->queue.stopQueue) {
6,821,481✔
1924
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1925
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1926
    }
1927

1928
    stmtEnqueue(pStmt, param);
6,821,227✔
1929

1930
    return TSDB_CODE_SUCCESS;
6,822,458✔
1931
  }
1932

1933
  STMT_ERR_RET(stmtCacheBlock(pStmt));
4,799✔
1934

1935
  return TSDB_CODE_SUCCESS;
4,799✔
1936
}
1937
/*
1938
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1939
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1940
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1941
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1942

1943
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1944
  pRes->fields = taosMemoryMalloc(size);
1945
  pRes->userFields = taosMemoryMalloc(size);
1946
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1947
    STMT_ERR_RET(terrno);
1948
  }
1949
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1950
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1951

1952
  return TSDB_CODE_SUCCESS;
1953
}
1954

1955
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1956
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1957
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1958

1959
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1960
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1961

1962
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1963
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1964
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1965
      STMT_ERR_RET(terrno);
1966
    }
1967
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1968
  }
1969

1970
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1971
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1972
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1973
      STMT_ERR_RET(terrno);
1974
    }
1975
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1976
  }
1977

1978
  return TSDB_CODE_SUCCESS;
1979
}
1980
*/
1981

1982
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
14,922,406✔
1983
  STscStmt2* pStmt = (STscStmt2*)stmt;
14,922,406✔
1984
  int32_t    code = 0;
14,922,406✔
1985

1986
  int64_t startUs = taosGetTimestampUs();
14,928,355✔
1987

1988
  if (qDebugFlag & DEBUG_TRACE) {
14,928,355✔
NEW
1989
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1990
  }
1991

1992
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
14,927,823✔
1993
    return pStmt->errCode;
×
1994
  }
1995

1996
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
14,927,823✔
1997

1998
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
14,927,924✔
1999
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2000
    pStmt->bInfo.needParse = false;
×
2001
  }
2002

2003
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
14,927,924✔
2004
    resetRequest(pStmt);
×
2005
  }
2006

2007
  STMT_ERR_RET(stmtCreateRequest(pStmt));
14,927,416✔
2008
  if (pStmt->bInfo.needParse) {
14,927,129✔
2009
    code = stmtParseSql(pStmt);
×
2010
    if (code != TSDB_CODE_SUCCESS) {
×
2011
      goto cleanup_root;
×
2012
    }
2013
  }
2014

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

2042
    if (pStmt->sql.pQuery->haveResultSet) {
223✔
2043
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
446✔
2044
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
2045
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
223✔
2046
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
223✔
2047
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
223✔
2048
    }
2049

2050
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
223✔
2051
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
223✔
2052
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
223✔
2053

2054
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
2055
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
2056
    // }
2057

2058
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2059

2060
    return TSDB_CODE_SUCCESS;
223✔
2061

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

2071
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
14,927,414✔
2072
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2073
  }
2074

2075
  STableDataCxt** pDataBlock = NULL;
14,926,790✔
2076

2077
  if (pStmt->exec.pCurrBlock) {
14,926,790✔
2078
    pDataBlock = &pStmt->exec.pCurrBlock;
14,907,260✔
2079
  } else {
2080
    pDataBlock =
2081
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,045✔
2082
    if (NULL == pDataBlock) {
20,045✔
2083
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
2084
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2085
    }
2086
    pStmt->exec.pCurrBlock = *pDataBlock;
20,045✔
2087
    if (pStmt->sql.stbInterlaceMode) {
20,045✔
2088
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
19,409✔
2089
      (*pDataBlock)->pData->aCol = NULL;
19,409✔
2090
    }
2091
    if (colIdx < -1) {
20,045✔
2092
      pStmt->sql.bindRowFormat = true;
×
2093
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
2094
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
2095
    }
2096
  }
2097

2098
  int64_t startUs2 = taosGetTimestampUs();
14,927,214✔
2099
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
14,927,214✔
2100

2101
  SStmtQNode* param = NULL;
14,925,944✔
2102
  if (pStmt->sql.stbInterlaceMode) {
14,925,944✔
2103
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
29,846,814✔
2104
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
29,848,379✔
2105
    taosArrayClear(param->tblData.aCol);
14,924,600✔
2106

2107
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2108

2109
    param->restoreTbCols = false;
14,917,182✔
2110
    param->tblData.isOrdered = true;
14,917,436✔
2111
    param->tblData.isDuplicateTs = false;
14,917,436✔
2112
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
14,917,944✔
2113

2114
    param->pCreateTbReq = pCreateTbReq;
14,917,436✔
2115
  }
2116

2117
  int64_t startUs3 = taosGetTimestampUs();
14,925,573✔
2118
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
14,925,573✔
2119

2120
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
14,924,811✔
2121
  SBlobSet* pBlob = NULL;
14,925,065✔
2122
  if (colIdx < 0) {
14,925,319✔
2123
    if (pStmt->sql.stbInterlaceMode) {
14,927,715✔
2124
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
14,922,498✔
2125
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
16,790,081✔
2126
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
14,923,260✔
2127
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
14,923,260✔
2128
      param->tblData.isOrdered = (*pDataBlock)->ordered;
14,924,674✔
2129
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
14,923,404✔
2130
    } else {
2131
      if (colIdx == -1) {
5,473✔
2132
        if (pStmt->sql.bindRowFormat) {
4,799✔
2133
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2134
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2135
        }
2136
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
4,799✔
2137
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
4,799✔
2138
      } else {
2139
        code =
2140
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
500✔
2141
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
500✔
2142
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
754✔
2143
      }
2144
    }
2145

2146
    if (code) {
14,928,965✔
2147
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
×
2148
      STMT_ERR_RET(code);
×
2149
    }
2150
  } else {
2151
    if (pStmt->sql.stbInterlaceMode) {
×
2152
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2153
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2154
    }
2155

2156
    if (pStmt->sql.bindRowFormat) {
×
2157
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2158
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2159
    }
2160

2161
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
2162
      STMT2_ELOG_E("bind column index not in sequence");
×
2163
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2164
    }
2165

2166
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2167

2168
    if (0 == colIdx) {
×
2169
      pStmt->bInfo.sBindRowNum = bind->num;
×
2170
    }
2171

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

2181
  int64_t startUs4 = taosGetTimestampUs();
14,929,369✔
2182
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
14,929,369✔
2183

2184
  if (pStmt->stbInterlaceMode) {
14,928,861✔
2185
    if (param) param->tblData.pBlobSet = pBlob;
14,925,347✔
2186
  }
2187

2188
  if (pStmt->sql.stbInterlaceMode) {
14,929,115✔
2189
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
14,925,090✔
2190
  } else {
2191
    STMT_ERR_RET(stmtAddBatch2(pStmt));
4,029✔
2192
  }
2193

2194
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
14,930,700✔
2195
  return TSDB_CODE_SUCCESS;
14,930,700✔
2196
}
2197

2198
/*
2199
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2200
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2201

2202
  int32_t code = 0;
2203
  int32_t finalCode = 0;
2204
  size_t  keyLen = 0;
2205
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2206
  while (pIter) {
2207
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2208
    char*          key = taosHashGetKey(pIter, &keyLen);
2209

2210
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2211
    if (pMeta->uid) {
2212
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2213
      continue;
2214
    }
2215

2216
    SSubmitBlkRsp* blkRsp = NULL;
2217
    int32_t        i = 0;
2218
    for (; i < pRsp->nBlocks; ++i) {
2219
      blkRsp = pRsp->pBlocks + i;
2220
      if (strlen(blkRsp->tblFName) != keyLen) {
2221
        continue;
2222
      }
2223

2224
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2225
        continue;
2226
      }
2227

2228
      break;
2229
    }
2230

2231
    if (i < pRsp->nBlocks) {
2232
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2233
               blkRsp->uid);
2234

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

2248
      code = stmtCreateRequest(pStmt);
2249
      if (code) {
2250
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2251
        finalCode = code;
2252
        continue;
2253
      }
2254

2255
      STableMeta*      pTableMeta = NULL;
2256
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2257
                               .requestId = pStmt->exec.pRequest->requestId,
2258
                               .requestObjRefId = pStmt->exec.pRequest->self,
2259
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2260
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2261

2262
      pStmt->stat.ctgGetTbMetaNum++;
2263

2264
      taos_free_result(pStmt->exec.pRequest);
2265
      pStmt->exec.pRequest = NULL;
2266

2267
      if (code || NULL == pTableMeta) {
2268
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2269
        finalCode = code;
2270
        taosMemoryFree(pTableMeta);
2271
        continue;
2272
      }
2273

2274
      pMeta->uid = pTableMeta->uid;
2275
      pStmt->bInfo.tbUid = pTableMeta->uid;
2276
      taosMemoryFree(pTableMeta);
2277
    }
2278

2279
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2280
  }
2281

2282
  return finalCode;
2283
}
2284
*/
2285
/*
2286
int stmtStaticModeExec(TAOS_STMT* stmt) {
2287
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2288
  int32_t     code = 0;
2289
  SSubmitRsp* pRsp = NULL;
2290
  if (pStmt->sql.staticMode) {
2291
    return TSDB_CODE_TSC_STMT_API_ERROR;
2292
  }
2293

2294
  STMT_DLOG_E("start to exec");
2295

2296
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2297

2298
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2299
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2300

2301
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2302

2303
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2304
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2305
    if (code) {
2306
      pStmt->exec.pRequest->code = code;
2307
    } else {
2308
      tFreeSSubmitRsp(pRsp);
2309
      STMT_ERR_RET(stmtResetStmt(pStmt));
2310
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2311
    }
2312
  }
2313

2314
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2315

2316
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2317
  pStmt->affectedRows += pStmt->exec.affectedRows;
2318

2319
_return:
2320

2321
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2322

2323
  tFreeSSubmitRsp(pRsp);
2324

2325
  ++pStmt->sql.runTimes;
2326

2327
  STMT_RET(code);
2328
}
2329
*/
2330

2331
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
2332
  const STscObj* pTscObj = pRequest->pTscObj;
×
2333

2334
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
2335
  if (*pCxt == NULL) {
×
2336
    return terrno;
×
2337
  }
2338

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

2365
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
2366
  STscStmt2*        pStmt = userdata;
×
2367
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
2368
  pStmt->asyncResultAvailable = true;
×
2369
  pStmt->exec.pRequest->inCallback = true;
×
2370

2371
  if (code == TSDB_CODE_SUCCESS) {
×
2372
    pStmt->exec.affectedRows = taos_affected_rows(res);
×
2373
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
2374
  }
2375

2376
  if (fp) {
×
2377
    fp(pStmt->options.userdata, res, code);
×
2378
  }
2379

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

2389
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
2390
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2391
  }
2392
}
×
2393

2394
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
6,821,369✔
2395
  STscStmt2* pStmt = (STscStmt2*)stmt;
6,821,369✔
2396
  int32_t    code = 0;
6,821,369✔
2397
  int64_t    startUs = taosGetTimestampUs();
6,822,470✔
2398

2399
  STMT2_DLOG_E("start to exec");
6,822,470✔
2400

2401
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6,822,548✔
2402
    return pStmt->errCode;
×
2403
  }
2404

2405
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
6,822,548✔
2406
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
6,823,161✔
2407
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2408
  }
2409
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
6,822,792✔
2410

2411
  if (pStmt->sql.stbInterlaceMode) {
6,822,822✔
2412
    STMT_ERR_RET(stmtAddBatch2(pStmt));
6,822,373✔
2413
  }
2414

2415
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
6,822,903✔
2416

2417
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
6,821,528✔
2418
    if (pStmt->sql.stbInterlaceMode) {
6,822,002✔
2419
      int64_t startTs = taosGetTimestampUs();
6,822,529✔
2420
      // wait for stmt bind thread to finish
2421
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
26,644,492✔
2422
        taosUsleep(1);
19,820,122✔
2423
      }
2424

2425
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6,822,093✔
2426
        return pStmt->errCode;
×
2427
      }
2428

2429
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
6,822,086✔
2430
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
6,822,086✔
2431
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
6,822,119✔
2432
      pStmt->sql.siInfo.pVgroupHash = NULL;
6,822,234✔
2433
      pStmt->sql.siInfo.pVgroupList = NULL;
6,822,234✔
2434
    } else {
2435
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
326✔
2436
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
636✔
2437

2438
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
636✔
2439

2440
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
636✔
2441
    }
2442
  }
2443

2444
  pStmt->asyncResultAvailable = false;
6,822,650✔
2445
  SRequestObj*      pRequest = pStmt->exec.pRequest;
6,822,650✔
2446
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
6,822,396✔
2447
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
6,822,650✔
2448
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2449

2450
  if (!fp) {
6,822,608✔
2451
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
6,822,608✔
2452

2453
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
6,821,735✔
2454
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2455
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2456
      if (code) {
×
2457
        pStmt->exec.pRequest->code = code;
×
2458

2459
      } else {
2460
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2461
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2462
      }
2463
    }
2464

2465
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
6,822,243✔
2466

2467
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
6,821,735✔
2468
    if (affected_rows) {
6,822,761✔
2469
      *affected_rows = pStmt->exec.affectedRows;
6,821,513✔
2470
    }
2471
    pStmt->affectedRows += pStmt->exec.affectedRows;
6,822,253✔
2472

2473
    // wait for stmt bind thread to finish
2474
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
6,826,239✔
2475
      taosUsleep(1);
3,478✔
2476
    }
2477

2478
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
6,821,659✔
2479

2480
    ++pStmt->sql.runTimes;
6,820,527✔
2481
  } else {
2482
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
2483
    if (pWrapper == NULL) {
×
2484
      code = terrno;
×
2485
    } else {
2486
      pWrapper->pRequest = pRequest;
×
2487
      pRequest->pWrapper = pWrapper;
×
2488
    }
2489
    if (TSDB_CODE_SUCCESS == code) {
×
2490
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
2491
    }
2492
    pRequest->syncQuery = false;
×
2493
    pRequest->body.queryFp = asyncQueryCb;
×
2494
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
2495

2496
    pStmt->execSemWaited = false;
×
2497
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
2498
  }
2499

2500
_return:
6,820,527✔
2501
  if (code) {
6,820,527✔
2502
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2503
  }
2504
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
6,821,402✔
2505

2506
  STMT_RET(code);
6,821,910✔
2507
}
2508

2509
int stmtClose2(TAOS_STMT2* stmt) {
18,420✔
2510
  STscStmt2* pStmt = (STscStmt2*)stmt;
18,420✔
2511

2512
  STMT2_DLOG_E("start to close stmt");
18,420✔
2513
  taosMemoryFreeClear(pStmt->db);
18,420✔
2514

2515
  if (pStmt->bindThreadInUse) {
18,420✔
2516
    // wait for stmt bind thread to finish
2517
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
17,561✔
2518
      taosUsleep(1);
×
2519
    }
2520

2521
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
17,561✔
2522
    pStmt->queue.stopQueue = true;
17,561✔
2523
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
17,561✔
2524
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
17,561✔
2525

2526
    (void)taosThreadJoin(pStmt->bindThread, NULL);
17,561✔
2527
    pStmt->bindThreadInUse = false;
17,561✔
2528

2529
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
17,561✔
2530
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
17,561✔
2531
  }
2532

2533
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
18,420✔
2534
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
18,420✔
2535
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2536
  }
2537
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
18,420✔
2538

2539
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
18,420✔
2540
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
18,420✔
2541

2542
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
18,420✔
2543
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2544
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2545
    }
2546
  }
2547

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

2563
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
18,420✔
2564

2565
  if (pStmt->options.asyncExecFn) {
18,420✔
2566
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
2567
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2568
    }
2569
  }
2570
  taosMemoryFree(stmt);
18,420✔
2571

2572
  return TSDB_CODE_SUCCESS;
18,420✔
2573
}
2574

2575
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
2576
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2577

2578
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
2579
    return (char*)tstrerror(terrno);
×
2580
  }
2581

2582
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2583
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
×
2584
    pStmt->exec.pRequest->code = terrno;
×
2585
  }
2586

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

2596
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2597
*/
2598

2599
int stmtParseColFields2(TAOS_STMT2* stmt) {
620✔
2600
  int32_t    code = 0;
620✔
2601
  STscStmt2* pStmt = (STscStmt2*)stmt;
620✔
2602
  int32_t    preCode = pStmt->errCode;
620✔
2603

2604
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
620✔
UNCOV
2605
    return pStmt->errCode;
×
2606
  }
2607

2608
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
620✔
2609
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2610
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2611
  }
2612

2613
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
620✔
2614

2615
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
620✔
2616
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2617
    pStmt->bInfo.needParse = false;
×
2618
  }
2619
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
620✔
2620
    pStmt->bInfo.needParse = false;
×
2621
  }
2622

2623
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
620✔
2624

2625
  if (pStmt->bInfo.needParse) {
620✔
2626
    STMT_ERRI_JRET(stmtParseSql(pStmt));
620✔
2627
  }
2628

2629
_return:
620✔
2630
  // compatible with previous versions
2631
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
620✔
2632
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2633
  }
2634

2635
  pStmt->errCode = preCode;
620✔
2636

2637
  return code;
620✔
2638
}
2639

2640
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
620✔
2641
  int32_t code = stmtParseColFields2(stmt);
620✔
2642
  if (code != TSDB_CODE_SUCCESS) {
620✔
2643
    return code;
×
2644
  }
2645

2646
  return stmtFetchStbColFields2(stmt, nums, fields);
620✔
2647
}
2648

2649
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2650
  int32_t    code = 0;
×
2651
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2652
  int32_t    preCode = pStmt->errCode;
×
2653

2654
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
UNCOV
2655
    return pStmt->errCode;
×
2656
  }
2657

2658
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2659

2660
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2661
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2662
    pStmt->bInfo.needParse = false;
×
2663
  }
2664

2665
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2666
    resetRequest(pStmt);
×
2667
  }
2668

2669
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2670

2671
  if (pStmt->bInfo.needParse) {
×
2672
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2673
  }
2674

2675
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2676
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2677
  } else {
2678
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2679
  }
2680

NEW
2681
  STMT2_DLOG("get param num success, nums:%d", *nums);
×
2682

2683
_return:
×
2684

2685
  pStmt->errCode = preCode;
×
2686

2687
  return code;
×
2688
}
2689

2690
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
223✔
2691
  STscStmt2* pStmt = (STscStmt2*)stmt;
223✔
2692

2693
  STMT2_TLOG_E("start to use result");
223✔
2694

2695
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
223✔
2696
    STMT2_ELOG_E("useResult only for query statement");
×
2697
    return NULL;
×
2698
  }
2699

2700
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
223✔
2701
    STMT2_ELOG_E("use result after callBackFn return");
×
2702
    return NULL;
×
2703
  }
2704

2705
  return pStmt->exec.pRequest;
223✔
2706
}
2707

2708
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2709
  qInfo("async stmt bind thread started");
×
2710

2711
  ThreadArgs* targs = (ThreadArgs*)args;
×
2712
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2713

2714
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2715
  targs->fp(targs->param, NULL, code);
×
2716
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2717
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2718
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2719
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2720
  taosMemoryFree(args);
×
2721

2722
  qInfo("async stmt bind thread stopped");
×
2723

2724
  return code;
×
2725
}
2726

2727
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
×
2728
  if (pStmt == NULL || msg == NULL) {
×
2729
    return;
×
2730
  }
2731

2732
  if (pStmt->exec.pRequest == NULL) {
×
2733
    return;
×
2734
  }
2735

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

2740
  size_t msgLen = strlen(msg);
×
2741
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
×
2742

2743
  if (msgLen >= bufLen) {
×
2744
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2745
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
2746
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2747
  } else {
2748
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
×
2749
    pStmt->exec.pRequest->msgBufLen = msgLen;
×
2750
  }
2751

2752
  return;
×
2753
}
2754

2755
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
×
2756
  stmtBuildErrorMsg(pStmt, msg);
×
2757
  pStmt->errCode = errorCode;
×
2758

2759
  return errorCode;
×
2760
}
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