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

taosdata / TDengine / #4907

30 Dec 2025 10:52AM UTC coverage: 65.541% (+0.03%) from 65.514%
#4907

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

808 existing lines in 106 files now uncovered.

193920 of 295877 relevant lines covered (65.54%)

118520209.34 hits per line

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

62.46
/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) {
1,698,545✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,698,520✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,698,292✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
70✔
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;
1,698,014✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,699,001✔
42
  int i = 0;
1,699,001✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
8,227,499✔
44
    if (pStmt->queue.stopQueue) {
6,532,058✔
45
      return false;
3,225✔
46
    }
47
    if (i < 10) {
6,528,303✔
48
      taosUsleep(1);
6,052,737✔
49
      i++;
6,052,141✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
475,566✔
52
      if (pStmt->queue.stopQueue) {
476,357✔
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
476,343✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
476,329✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
476,241✔
60
    }
61
  }
62

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

67
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,695,018✔
68
  if (pStmt->queue.head == pStmt->queue.tail) {
1,695,934✔
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;
1,695,828✔
76
  pStmt->queue.head->next = node->next;
1,695,874✔
77
  if (pStmt->queue.tail == node) {
1,695,842✔
78
    pStmt->queue.tail = pStmt->queue.head;
1,125,397✔
79
  }
80
  node->next = NULL;
1,695,749✔
81
  *param = node;
1,695,702✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,695,794✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,695,894✔
85

86
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
1,695,845✔
87

88
  return true;
1,695,846✔
89
}
90

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

97
  param->next = NULL;
1,695,214✔
98

99
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,695,353✔
100

101
  pStmt->queue.tail->next = param;
1,695,744✔
102
  pStmt->queue.tail = param;
1,695,651✔
103
  pStmt->stat.bindDataNum++;
1,695,560✔
104

105
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,695,652✔
106
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,695,813✔
107

108
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,695,777✔
109

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

114
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
2,966,389✔
115
  int32_t code = 0;
2,966,389✔
116

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

136
  return code;
2,966,863✔
137
}
138

139
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
4,069,380✔
140
  int32_t code = 0;
4,069,380✔
141

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
4,069,380✔
143
    STMT2_LOG_SEQ(newStatus);
4,070,911✔
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
4,070,004✔
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) {
4,070,421✔
152
    case STMT_PREPARE:
3,255✔
153
      pStmt->errCode = 0;
3,255✔
154
      break;
2,871✔
155
    case STMT_SETTBNAME:
1,142,097✔
156
      if (STMT_STATUS_EQ(INIT)) {
1,142,097✔
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
1,142,144✔
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
1,142,053✔
163
    case STMT_SETTAGS:
677,234✔
164
      if (STMT_STATUS_EQ(INIT)) {
677,234✔
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
677,234✔
168
    case STMT_FETCH_FIELDS:
22✔
169
      if (STMT_STATUS_EQ(INIT)) {
22✔
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
22✔
173
    case STMT_BIND:
1,141,913✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
1,141,913✔
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;
1,142,372✔
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:
552,834✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
552,834✔
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
552,880✔
193
    case STMT_EXECUTE:
553,066✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
553,066✔
195
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
8✔
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)) {
552,874✔
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
202
        }
203
      }
204
      break;
552,882✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
4,070,314✔
211

212
  pStmt->sql.status = newStatus;
4,070,314✔
213

214
  return TSDB_CODE_SUCCESS;
4,070,681✔
215
}
216

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

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
3,269✔
221

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

227
  *tbName = pStmt->bInfo.tbName;
3,247✔
228

229
  return TSDB_CODE_SUCCESS;
3,247✔
230
}
231

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

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

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

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

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

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

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

285
  return TSDB_CODE_SUCCESS;
3,209✔
286
}
287

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

291
  pStmt->sql.pVgHash = pVgHash;
3,209✔
292
  pStmt->exec.pBlockHash = pBlockHash;
3,209✔
293

294
  return TSDB_CODE_SUCCESS;
3,154✔
295
}
296

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

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

305
  pStmt->sql.autoCreateTbl = autoCreateTbl;
3,108✔
306

307
  return TSDB_CODE_SUCCESS;
3,214✔
308
}
309

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

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

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

319
  return TSDB_CODE_SUCCESS;
22✔
320
}
321

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

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

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

335
  pStmt->stat.parseSqlNum++;
3,277✔
336

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

340
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
3,277✔
341

342
  pStmt->bInfo.needParse = false;
3,277✔
343

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

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

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

402
  return TSDB_CODE_SUCCESS;
3,269✔
403
}
404

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

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

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

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

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

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

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

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

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

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

525
  return TSDB_CODE_SUCCESS;
×
526
}
527

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

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

543
  pStmt->bInfo.tbName[0] = 0;
559,372✔
544
  pStmt->bInfo.tbFName[0] = 0;
559,372✔
545
  if (!pStmt->bInfo.tagsCached) {
559,189✔
546
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
334,081✔
547
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
333,961✔
548
  }
549

550
  if (!pStmt->bInfo.boundColsCached) {
559,206✔
551
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
6,308✔
552
    pStmt->bInfo.boundCols = NULL;
6,308✔
553
  }
554

555
  if (!pStmt->sql.autoCreateTbl) {
559,114✔
556
    pStmt->bInfo.stbFName[0] = 0;
333,927✔
557
    pStmt->bInfo.tbSuid = 0;
334,018✔
558
  }
559

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

563
  return TSDB_CODE_SUCCESS;
559,010✔
564
}
565

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

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

580
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
552,812✔
581
  pQueue->qRemainNum = 0;
552,720✔
582
  pQueue->head->next = NULL;
552,720✔
583
}
584

585
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
555,646✔
586
  if (pStmt->sql.stbInterlaceMode) {
555,646✔
587
    if (deepClean) {
555,821✔
588
      taosHashCleanup(pStmt->exec.pBlockHash);
3,225✔
589
      pStmt->exec.pBlockHash = NULL;
3,225✔
590

591
      if (NULL != pStmt->exec.pCurrBlock) {
3,225✔
592
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
3,225✔
593
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
3,225✔
594
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
3,225✔
595
        pStmt->exec.pCurrBlock = NULL;
3,225✔
596
      }
597
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
3,225✔
598
        resetRequest(pStmt);
3,225✔
599
      }
600
    } else {
601
      pStmt->sql.siInfo.pTableColsIdx = 0;
552,596✔
602
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
552,688✔
603
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
552,794✔
604
    }
605
    if (NULL != pStmt->exec.pRequest) {
556,205✔
606
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
552,980✔
607
    }
608
  } else {
609
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
98✔
UNCOV
610
      resetRequest(pStmt);
×
611
    }
612

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

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

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

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

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

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

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

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

647
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
556,235✔
648
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
555,933✔
649
             keepTable, deepClean);
650

651
  return TSDB_CODE_SUCCESS;
555,982✔
652
}
653

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

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

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

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

679
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
3,143✔
680
  while (pIter) {
3,165✔
681
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
22✔
682

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

687
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
22✔
688
  }
689
  taosHashCleanup(pStmt->sql.pTableCache);
3,143✔
690
  pStmt->sql.pTableCache = NULL;
3,143✔
691

692
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
3,143✔
693
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,143✔
694

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

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

708
  STMT2_TLOG_E("end to free SQL info");
3,143✔
709

710
  return TSDB_CODE_SUCCESS;
3,143✔
711
}
712

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

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

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

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

737
  *vgId = vgInfo.vgId;
677,016✔
738

739
  return TSDB_CODE_SUCCESS;
677,016✔
740
}
741

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

750
  pStmt->stat.ctgGetTbMetaNum++;
3,036✔
751

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

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

761
    STMT_ERR_RET(code);
×
762
  }
763

764
  STMT_ERR_RET(code);
3,036✔
765

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

772
  taosMemoryFree(pTableMeta);
3,036✔
773

774
  return TSDB_CODE_SUCCESS;
3,036✔
775
}
776

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

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

784
  return TSDB_CODE_SUCCESS;
142✔
785
}
786

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

794
  pStmt->bInfo.needParse = true;
3,389✔
795
  pStmt->bInfo.inExecCache = false;
3,329✔
796

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

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

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

810
  if (NULL == pStmt->pCatalog) {
3,389✔
811
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
3,135✔
812
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
3,135✔
813
  }
814

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

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

824
    return TSDB_CODE_SUCCESS;
3,247✔
825
  }
826

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

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

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

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

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

845
      return TSDB_CODE_SUCCESS;
142✔
846
    }
847

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

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

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

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

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

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

864
    return TSDB_CODE_SUCCESS;
×
865
  }
866

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

873
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
874
    }
875

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

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

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

886
    return TSDB_CODE_SUCCESS;
×
887
  }
888

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

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

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

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

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

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

911
    return TSDB_CODE_SUCCESS;
×
912
  }
913

914
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
915

916
  return TSDB_CODE_SUCCESS;
×
917
}
918

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

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

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

930
  return TSDB_CODE_SUCCESS;
×
931
}
932

933
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
1,695,608✔
934
  SStmtQNode* pParam = (SStmtQNode*)param;
1,695,608✔
935

936
  if (pParam->restoreTbCols) {
1,695,608✔
937
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,695,818✔
938
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
1,142,870✔
939
      *p = taosArrayInit(20, POINTER_BYTES);
1,142,838✔
940
      if (*p == NULL) {
1,142,838✔
UNCOV
941
        pStmt->errCode = terrno;
×
942
      }
943
    }
944
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
552,994✔
945
    STMT2_TLOG_E("restore pTableCols finished");
553,036✔
946
  } else {
947
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
1,142,670✔
948
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
949
    // taosMemoryFree(pParam->pTbData);
950
    if (code != TSDB_CODE_SUCCESS) {
1,142,744✔
951
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
952
      pStmt->errCode = code;
×
953
    }
954
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,142,744✔
955
  }
956
}
1,695,920✔
957

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

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

964
  while (true) {
1,695,892✔
965
    SStmtQNode* asyncParam = NULL;
1,699,117✔
966

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

975
    stmtAsyncOutput(pStmt, asyncParam);
1,695,572✔
976
  }
977

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

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

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

996
  pStmt->bindThreadInUse = true;
3,225✔
997

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

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

1008
  return TSDB_CODE_SUCCESS;
3,225✔
1009
}
1010

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

1016
  return TSDB_CODE_SUCCESS;
3,143✔
1017
}
1018

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

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

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

1039
  return TSDB_CODE_SUCCESS;
3,225✔
1040
}
1041

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

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

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

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

1068
  if (NULL != pOptions) {
3,143✔
1069
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
3,121✔
1070
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
3,121✔
1071
      pStmt->stbInterlaceMode = true;
3,113✔
1072
    }
1073

1074
    pStmt->reqid = pOptions->reqid;
3,121✔
1075
  }
1076

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

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

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

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

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

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

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

1137
  pStmt->execSemWaited = false;
3,143✔
1138

1139
  // STMT_LOG_SEQ(STMT_INIT);
1140

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

1144
  return pStmt;
3,143✔
1145
}
1146

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

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

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

1178
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
112✔
1179
  if (NULL == pStmt->sql.siInfo.pTableHash) {
112✔
1180
    return terrno;
×
1181
  }
1182

1183
  pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
112✔
1184
  if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
112✔
1185
    return terrno;
×
1186
  }
1187

1188
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
112✔
1189
  if (NULL == pStmt->sql.siInfo.pTableCols) {
112✔
1190
    return terrno;
×
1191
  }
1192

1193
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
112✔
1194

1195
  if (TSDB_CODE_SUCCESS == code) {
112✔
1196
    code = stmtInitQueue(pStmt);
112✔
1197
    pStmt->queue.stopQueue = false;
112✔
1198
  }
1199
  if (TSDB_CODE_SUCCESS == code) {
112✔
1200
    code = stmtStartBindThread(pStmt);
112✔
1201
  }
1202
  if (TSDB_CODE_SUCCESS != code) {
112✔
1203
    return code;
×
1204
  }
1205

1206
  return TSDB_CODE_SUCCESS;
112✔
1207
}
1208

1209
static int32_t stmtDeepReset(STscStmt2* pStmt) {
112✔
1210
  char*             db = pStmt->db;
112✔
1211
  TAOS_STMT2_OPTION options = pStmt->options;
112✔
1212
  uint32_t          reqid = pStmt->reqid;
112✔
1213

1214
  pStmt->errCode = 0;
112✔
1215
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
112✔
1216
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1217
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1218
    }
1219
    pStmt->execSemWaited = true;
×
1220
  }
1221

1222
  if (pStmt->stbInterlaceMode) {
112✔
1223
    if (pStmt->bindThreadInUse) {
112✔
1224
      while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
112✔
1225
        taosUsleep(1);
×
1226
      }
1227
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
112✔
1228
      pStmt->queue.stopQueue = true;
112✔
1229
      (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
112✔
1230
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
112✔
1231

1232
      (void)taosThreadJoin(pStmt->bindThread, NULL);
112✔
1233
      pStmt->bindThreadInUse = false;
112✔
1234
      pStmt->queue.head = NULL;
112✔
1235
      pStmt->queue.tail = NULL;
112✔
1236
      pStmt->queue.qRemainNum = 0;
112✔
1237

1238
      (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
112✔
1239
      (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
112✔
1240
    }
1241
  }
1242

1243
  pStmt->sql.autoCreateTbl = false;
112✔
1244
  taosMemoryFree(pStmt->sql.pBindInfo);
112✔
1245
  pStmt->sql.pBindInfo = NULL;
112✔
1246

1247
  taosMemoryFree(pStmt->sql.queryRes.fields);
112✔
1248
  pStmt->sql.queryRes.fields = NULL;
112✔
1249

1250
  taosMemoryFree(pStmt->sql.queryRes.userFields);
112✔
1251
  pStmt->sql.queryRes.userFields = NULL;
112✔
1252

1253
  pStmt->sql.type = 0;
112✔
1254
  pStmt->sql.runTimes = 0;
112✔
1255
  taosMemoryFree(pStmt->sql.sqlStr);
112✔
1256
  pStmt->sql.sqlStr = NULL;
112✔
1257

1258
  qDestroyQuery(pStmt->sql.pQuery);
112✔
1259
  pStmt->sql.pQuery = NULL;
112✔
1260

1261
  taosArrayDestroy(pStmt->sql.nodeList);
112✔
1262
  pStmt->sql.nodeList = NULL;
112✔
1263

1264
  taosHashCleanup(pStmt->sql.pVgHash);
112✔
1265
  pStmt->sql.pVgHash = NULL;
112✔
1266

1267
  if (pStmt->sql.fixValueTags) {
112✔
1268
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
112✔
1269
    pStmt->sql.fixValueTbReq = NULL;
112✔
1270
  }
1271
  pStmt->sql.fixValueTags = false;
112✔
1272

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

1277
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
1278
    qDestroyBoundColInfo(pCache->boundTags);
×
1279
    taosMemoryFreeClear(pCache->boundTags);
×
1280

1281
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
1282
  }
1283
  taosHashCleanup(pStmt->sql.pTableCache);
112✔
1284

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

1290
  resetRequest(pStmt);
112✔
1291

1292
  if (pStmt->sql.siInfo.pTableCols) {
112✔
1293
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
112✔
1294
    pStmt->sql.siInfo.pTableCols = NULL;
112✔
1295
  }
1296

1297
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
112✔
1298
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
112✔
1299
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
112✔
1300
  }
1301

1302
  if (pStmt->sql.siInfo.pTableHash) {
112✔
1303
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
112✔
1304
    pStmt->sql.siInfo.pTableHash = NULL;
112✔
1305
  }
1306

1307
  if (pStmt->sql.siInfo.pTableRowDataHash) {
112✔
1308
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
112✔
1309
    pStmt->sql.siInfo.pTableRowDataHash = NULL;
112✔
1310
  }
1311

1312
  if (pStmt->sql.siInfo.pVgroupHash) {
112✔
1313
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1314
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1315
  }
1316

1317
  if (pStmt->sql.siInfo.pVgroupList) {
112✔
1318
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
1319
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1320
  }
1321

1322
  if (pStmt->sql.siInfo.pDataCtx) {
112✔
1323
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
112✔
1324
    pStmt->sql.siInfo.pDataCtx = NULL;
112✔
1325
  }
1326

1327
  if (pStmt->sql.siInfo.pTSchema) {
112✔
1328
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
112✔
1329
    pStmt->sql.siInfo.pTSchema = NULL;
112✔
1330
  }
1331

1332
  if (pStmt->sql.siInfo.pRequest) {
112✔
1333
    taos_free_result(pStmt->sql.siInfo.pRequest);
112✔
1334
    pStmt->sql.siInfo.pRequest = NULL;
112✔
1335
  }
1336

1337
  if (pStmt->stbInterlaceMode) {
112✔
1338
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
112✔
1339
  }
1340

1341
  pStmt->db = db;
112✔
1342
  pStmt->options = options;
112✔
1343
  pStmt->reqid = reqid;
112✔
1344

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

1351
  pStmt->sql.status = STMT_INIT;
112✔
1352

1353
  return TSDB_CODE_SUCCESS;
112✔
1354
}
1355

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

1360
  STMT2_DLOG("start to prepare with sql:%s", sql);
3,255✔
1361

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

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

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

1378
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
3,255✔
1379

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

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

1405
      if (pStmt->sql.stbInterlaceMode) {
3,247✔
1406
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
3,225✔
1407
      }
1408
    }
1409

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

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

1426
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3,225✔
1427
  pStmt->sql.siInfo.pDataCtx = pDst;
3,225✔
1428

1429
  SArray* pTblCols = NULL;
3,225✔
1430
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
3,200,376✔
1431
    pTblCols = taosArrayInit(20, POINTER_BYTES);
3,197,151✔
1432
    if (NULL == pTblCols) {
3,198,634✔
1433
      return terrno;
×
1434
    }
1435

1436
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
6,406,737✔
1437
      return terrno;
×
1438
    }
1439
  }
1440

1441
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
3,225✔
1442

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

1446
  return TSDB_CODE_SUCCESS;
3,225✔
1447
}
1448

1449
bool stmt2IsInsert(TAOS_STMT2* stmt) {
1,145,262✔
1450
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,145,262✔
1451
  if (pStmt->sql.type) {
1,145,262✔
1452
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,139,003✔
1453
  }
1454

1455
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
6,356✔
1456
}
1457

1458
bool stmt2IsSelect(TAOS_STMT2* stmt) {
3,233✔
1459
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,233✔
1460

1461
  if (pStmt->sql.type) {
3,233✔
1462
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1463
  }
1464
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
3,233✔
1465
}
1466

1467
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
1,142,235✔
1468
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,142,235✔
1469

1470
  int64_t startUs = taosGetTimestampUs();
1,142,347✔
1471

1472
  STMT2_TLOG("start to set tbName:%s", tbName);
1,142,347✔
1473

1474
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,142,403✔
1475
    return pStmt->errCode;
×
1476
  }
1477

1478
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
1,142,448✔
1479

1480
  int32_t insert = 0;
1,142,460✔
1481
  if (!stmt2IsInsert(stmt)) {
1,142,460✔
1482
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
1483
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1484
  }
1485
  // process tbname
1486
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,142,305✔
1487

1488
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,142,168✔
1489
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1490
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,142,318✔
1491
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
1,142,273✔
1492

1493
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
1,142,114✔
1494
    STMT_ERR_RET(stmtGetFromCache(pStmt));
3,305✔
1495

1496
    if (pStmt->bInfo.needParse) {
3,389✔
1497
      STMT_ERR_RET(stmtParseSql(pStmt));
3,247✔
1498
      if (!pStmt->sql.autoCreateTbl) {
3,247✔
1499
        uint64_t uid, suid;
2,968✔
1500
        int32_t  vgId;
2,968✔
1501
        int8_t   tableType;
2,968✔
1502

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

1510
  } else {
1511
    pStmt->exec.pRequest->requestId++;
1,138,718✔
1512
    pStmt->bInfo.needParse = false;
1,138,670✔
1513
  }
1514

1515
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,142,109✔
1516
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
3,225✔
1517
  }
1518

1519
  int64_t startUs2 = taosGetTimestampUs();
1,142,714✔
1520
  pStmt->stat.setTbNameUs += startUs2 - startUs;
1,142,714✔
1521

1522
  return TSDB_CODE_SUCCESS;
1,142,669✔
1523
}
1524

1525
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
565,206✔
1526
  STscStmt2* pStmt = (STscStmt2*)stmt;
565,206✔
1527

1528
  STMT2_TLOG_E("start to set tbTags");
565,206✔
1529
  if (qDebugFlag & DEBUG_TRACE) {
565,206✔
1530
    (void)stmtPrintBindv(stmt, tags, -1, true);
×
1531
  }
1532

1533
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
565,290✔
1534
    return pStmt->errCode;
×
1535
  }
1536

1537
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
565,290✔
1538

1539
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
565,290✔
1540
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1541
    pStmt->bInfo.needParse = false;
×
1542
  }
1543
  STMT_ERR_RET(stmtCreateRequest(pStmt));
565,290✔
1544

1545
  if (pStmt->bInfo.needParse) {
565,136✔
1546
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1547
  }
1548
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
565,136✔
1549
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1550
  }
1551

1552
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
565,164✔
1553

1554
  STableDataCxt** pDataBlock = NULL;
565,164✔
1555
  if (pStmt->exec.pCurrBlock) {
565,164✔
1556
    pDataBlock = &pStmt->exec.pCurrBlock;
565,093✔
1557
  } else {
1558
    pDataBlock =
1559
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
71✔
1560
    if (NULL == pDataBlock) {
71✔
1561
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1562
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1563
    }
1564
  }
1565
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
565,164✔
1566
    return TSDB_CODE_SUCCESS;
×
1567
  }
1568

1569
  STMT2_TLOG_E("start to bind stmt tag values");
565,164✔
1570

1571
  void* boundTags = NULL;
565,052✔
1572
  if (pStmt->sql.stbInterlaceMode) {
565,052✔
1573
    boundTags = pStmt->sql.siInfo.boundTags;
564,888✔
1574
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
564,888✔
1575
    if (NULL == pCreateTbReq) {
565,084✔
1576
      return terrno;
×
1577
    }
1578
    int32_t vgId = -1;
565,084✔
1579
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
565,084✔
1580
    (*pCreateTbReq)->uid = vgId;
565,252✔
1581
  } else {
1582
    boundTags = pStmt->bInfo.boundTags;
164✔
1583
  }
1584

1585
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
565,416✔
1586
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1587
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1588

1589
  return TSDB_CODE_SUCCESS;
565,332✔
1590
}
1591

1592
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
111,860✔
1593
  STscStmt2* pStmt = (STscStmt2*)stmt;
111,860✔
1594

1595
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
111,860✔
1596

1597
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
111,930✔
1598
    return pStmt->errCode;
×
1599
  }
1600

1601
  if (!pStmt->sql.stbInterlaceMode) {
111,930✔
1602
    return TSDB_CODE_SUCCESS;
×
1603
  }
1604

1605
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
111,930✔
1606

1607
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
111,916✔
1608
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1609
    pStmt->bInfo.needParse = false;
×
1610
  }
1611
  STMT_ERR_RET(stmtCreateRequest(pStmt));
111,916✔
1612

1613
  if (pStmt->bInfo.needParse) {
111,832✔
1614
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1615
    if (!pStmt->sql.autoCreateTbl) {
×
1616
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1617
      return TSDB_CODE_SUCCESS;
×
1618
    }
1619
  }
1620

1621
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
111,832✔
1622
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1623
  }
1624

1625
  STableDataCxt** pDataBlock = NULL;
111,790✔
1626
  if (pStmt->exec.pCurrBlock) {
111,790✔
1627
    pDataBlock = &pStmt->exec.pCurrBlock;
111,650✔
1628
  } else {
1629
    pDataBlock =
1630
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
140✔
1631
    if (NULL == pDataBlock) {
140✔
1632
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1633
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
112✔
1634
    }
1635
  }
1636

1637
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
111,902✔
1638
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1639
    return TSDB_CODE_SUCCESS;
×
1640
  }
1641

1642
  if (pStmt->sql.fixValueTags) {
111,902✔
1643
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
111,734✔
1644
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
111,734✔
1645
    if ((*pCreateTbReq)->name) {
111,790✔
1646
      taosMemoryFree((*pCreateTbReq)->name);
111,804✔
1647
    }
1648
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
111,776✔
1649
    int32_t vgId = -1;
111,790✔
1650
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
111,790✔
1651
    (*pCreateTbReq)->uid = vgId;
111,790✔
1652
    return TSDB_CODE_SUCCESS;
111,790✔
1653
  }
1654

1655
  if ((*pDataBlock)->pData->pCreateTbReq) {
168✔
1656
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
140✔
1657
    pStmt->sql.fixValueTags = true;
140✔
1658
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
140✔
1659
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
140✔
1660
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
140✔
1661
  }
1662

1663
  return TSDB_CODE_SUCCESS;
168✔
1664
}
1665

1666
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1667
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1668
    return pStmt->errCode;
×
1669
  }
1670

1671
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1672
    tscError("invalid operation to get query column fileds");
×
1673
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1674
  }
1675

1676
  STableDataCxt** pDataBlock = NULL;
×
1677

1678
  if (pStmt->sql.stbInterlaceMode) {
×
1679
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1680
  } else {
1681
    pDataBlock =
1682
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1683
    if (NULL == pDataBlock) {
×
1684
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1685
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1686
    }
1687
  }
1688

1689
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1690

1691
  return TSDB_CODE_SUCCESS;
×
1692
}
1693

1694
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
22✔
1695
  int32_t code = 0;
22✔
1696
  int32_t preCode = pStmt->errCode;
22✔
1697

1698
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
22✔
1699
    return pStmt->errCode;
×
1700
  }
1701

1702
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
22✔
1703
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1704
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1705
  }
1706

1707
  STableDataCxt** pDataBlock = NULL;
22✔
1708
  bool            cleanStb = false;
22✔
1709

1710
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
22✔
1711
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1712
  } else {
1713
    cleanStb = true;
22✔
1714
    pDataBlock =
1715
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
22✔
1716
  }
1717

1718
  if (NULL == pDataBlock || NULL == *pDataBlock) {
22✔
1719
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1720
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1721
  }
1722

1723
  pStmt->sql.placeholderOfTags = 0;
22✔
1724
  pStmt->sql.placeholderOfCols = 0;
22✔
1725
  int32_t totalNum = 0;
22✔
1726
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.boundCols,
22✔
1727
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1728
                                        &pStmt->sql.placeholderOfCols));
1729

1730
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
22✔
1731
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
22✔
1732
    qDestroyStmtDataBlock(*pDataBlock);
22✔
1733
    *pDataBlock = NULL;
22✔
1734
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
22✔
1735
      STMT2_ELOG("fail to remove remove stb:%s exec blockHash", pStmt->bInfo.tbFName);
×
1736
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1737
    }
1738
    pStmt->sql.autoCreateTbl = false;
22✔
1739
    pStmt->bInfo.tagsCached = false;
22✔
1740
    pStmt->bInfo.sname = (SName){0};
22✔
1741
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
22✔
1742
  }
1743

1744
  if (fieldNum != NULL) {
22✔
1745
    *fieldNum = totalNum;
22✔
1746
  }
1747

1748
  STMT2_DLOG("get insert fields totalNum:%d, tagsNum:%d, colsNum:%d", totalNum, pStmt->sql.placeholderOfTags,
22✔
1749
             pStmt->sql.placeholderOfCols);
1750

1751
_return:
22✔
1752

1753
  pStmt->errCode = preCode;
22✔
1754

1755
  return code;
22✔
1756
}
1757
/*
1758
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1759
  while (true) {
1760
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1761
      pStmt->exec.smInfo.pColIdx = 0;
1762
    }
1763

1764
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1765
      taosUsleep(1);
1766
      continue;
1767
    }
1768

1769
    *idx = pStmt->exec.smInfo.pColIdx;
1770
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1771
  }
1772
}
1773
*/
1774
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
1,142,334✔
1775
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
1,142,334✔
1776
    pStmt->sql.siInfo.pVgroupHash =
552,994✔
1777
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
552,768✔
1778
  }
1779
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
1,142,744✔
1780
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
552,994✔
1781
  }
1782

1783
  if (NULL == pStmt->sql.siInfo.pRequest) {
1,142,660✔
1784
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
3,225✔
1785
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1786

1787
    if (pStmt->reqid != 0) {
3,225✔
1788
      pStmt->reqid++;
8✔
1789
    }
1790
    pStmt->exec.pRequest->syncQuery = true;
3,225✔
1791

1792
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
3,225✔
1793
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
3,225✔
1794
  }
1795

1796
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
1,142,660✔
1797
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
20,461✔
1798
    pStmt->sql.siInfo.tbFromHash = true;
1,971✔
1799
  }
1800

1801
  if (0 == pStmt->sql.siInfo.firstName[0]) {
1,142,382✔
1802
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
3,113✔
1803
  }
1804

1805
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
1,142,521✔
1806
  param->next = NULL;
1,142,428✔
1807

1808
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,142,475✔
1809

1810
  if (pStmt->queue.stopQueue) {
1,142,912✔
1811
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1812
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1813
  }
1814
  stmtEnqueue(pStmt, param);
1,142,912✔
1815

1816
  return TSDB_CODE_SUCCESS;
1,142,736✔
1817
}
1818

1819
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1820
  while (true) {
1821
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
1,142,265✔
1822
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
1,142,631✔
1823
      break;
1,142,667✔
1824
    } else {
1825
      SArray* pTblCols = NULL;
×
1826
      for (int32_t i = 0; i < 100; i++) {
×
1827
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1828
        if (NULL == pTblCols) {
×
1829
          return terrno;
×
1830
        }
1831

1832
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1833
          return terrno;
×
1834
        }
1835
      }
1836
    }
1837
  }
1838

1839
  return TSDB_CODE_SUCCESS;
1,142,667✔
1840
}
1841

1842
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
164✔
1843
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
164✔
1844
    return TSDB_CODE_SUCCESS;
×
1845
  }
1846

1847
  uint64_t uid = pStmt->bInfo.tbUid;
164✔
1848
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
164✔
1849

1850
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
164✔
1851
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
142✔
1852
    return TSDB_CODE_SUCCESS;
142✔
1853
  }
1854

1855
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
22✔
1856
  if (!pSrc) {
22✔
1857
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1858
    return terrno;
×
1859
  }
1860
  STableDataCxt* pDst = NULL;
22✔
1861

1862
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
22✔
1863

1864
  SStmtTableCache cache = {
22✔
1865
      .pDataCtx = pDst,
1866
      .boundTags = pStmt->bInfo.boundTags,
22✔
1867
  };
1868

1869
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
22✔
1870
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
1871
    return terrno;
×
1872
  }
1873

1874
  if (pStmt->sql.autoCreateTbl) {
22✔
1875
    pStmt->bInfo.tagsCached = true;
22✔
1876
  } else {
1877
    pStmt->bInfo.boundTags = NULL;
×
1878
  }
1879

1880
  return TSDB_CODE_SUCCESS;
22✔
1881
}
1882

1883
static int stmtAddBatch2(TAOS_STMT2* stmt) {
552,904✔
1884
  STscStmt2* pStmt = (STscStmt2*)stmt;
552,904✔
1885

1886
  int64_t startUs = taosGetTimestampUs();
553,066✔
1887

1888
  // STMT2_TLOG_E("start to add batch");
1889

1890
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
553,066✔
1891
    return pStmt->errCode;
×
1892
  }
1893

1894
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
553,066✔
1895

1896
  if (pStmt->sql.stbInterlaceMode) {
552,968✔
1897
    int64_t startUs2 = taosGetTimestampUs();
552,888✔
1898
    pStmt->stat.addBatchUs += startUs2 - startUs;
552,888✔
1899

1900
    pStmt->sql.siInfo.tableColsReady = false;
552,842✔
1901

1902
    SStmtQNode* param = NULL;
552,842✔
1903
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,105,505✔
1904
    param->restoreTbCols = true;
552,754✔
1905
    param->next = NULL;
552,754✔
1906

1907
    if (pStmt->sql.autoCreateTbl) {
552,800✔
1908
      pStmt->bInfo.tagsCached = true;
224,966✔
1909
    }
1910
    pStmt->bInfo.boundColsCached = true;
552,754✔
1911

1912
    if (pStmt->queue.stopQueue) {
552,800✔
1913
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1914
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1915
    }
1916

1917
    stmtEnqueue(pStmt, param);
552,754✔
1918

1919
    return TSDB_CODE_SUCCESS;
552,856✔
1920
  }
1921

1922
  STMT_ERR_RET(stmtCacheBlock(pStmt));
164✔
1923

1924
  return TSDB_CODE_SUCCESS;
164✔
1925
}
1926
/*
1927
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1928
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1929
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1930
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1931

1932
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1933
  pRes->fields = taosMemoryMalloc(size);
1934
  pRes->userFields = taosMemoryMalloc(size);
1935
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1936
    STMT_ERR_RET(terrno);
1937
  }
1938
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1939
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1940

1941
  return TSDB_CODE_SUCCESS;
1942
}
1943

1944
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1945
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1946
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1947

1948
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1949
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1950

1951
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1952
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1953
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1954
      STMT_ERR_RET(terrno);
1955
    }
1956
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1957
  }
1958

1959
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1960
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1961
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1962
      STMT_ERR_RET(terrno);
1963
    }
1964
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1965
  }
1966

1967
  return TSDB_CODE_SUCCESS;
1968
}
1969
*/
1970

1971
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
1,142,232✔
1972
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,142,232✔
1973
  int32_t    code = 0;
1,142,232✔
1974

1975
  int64_t startUs = taosGetTimestampUs();
1,142,866✔
1976

1977
  if (qDebugFlag & DEBUG_TRACE) {
1,142,866✔
1978
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1979
  }
1980

1981
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,142,684✔
1982
    return pStmt->errCode;
×
1983
  }
1984

1985
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
1,142,179✔
1986

1987
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,142,300✔
1988
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1989
    pStmt->bInfo.needParse = false;
×
1990
  }
1991

1992
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,142,115✔
1993
    resetRequest(pStmt);
×
1994
  }
1995

1996
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,142,296✔
1997
  if (pStmt->bInfo.needParse) {
1,142,139✔
1998
    code = stmtParseSql(pStmt);
×
1999
    if (code != TSDB_CODE_SUCCESS) {
×
2000
      goto cleanup_root;
×
2001
    }
2002
  }
2003

2004
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,141,676✔
2005
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
8✔
2006
    if (code != TSDB_CODE_SUCCESS) {
8✔
2007
      goto cleanup_root;
×
2008
    }
2009
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
8✔
2010
                         .acctId = pStmt->taos->acctId,
8✔
2011
                         .db = pStmt->exec.pRequest->pDb,
8✔
2012
                         .topicQuery = false,
2013
                         .pSql = pStmt->sql.sqlStr,
8✔
2014
                         .sqlLen = pStmt->sql.sqlLen,
8✔
2015
                         .pMsg = pStmt->exec.pRequest->msgBuf,
8✔
2016
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2017
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
8✔
2018
                         .pStmtCb = NULL,
2019
                         .pUser = pStmt->taos->user,
8✔
2020
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
8✔
2021
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
8✔
2022
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
8✔
2023
    if (code != TSDB_CODE_SUCCESS) {
8✔
2024
      goto cleanup_root;
×
2025
    }
2026
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
8✔
2027
    if (code != TSDB_CODE_SUCCESS) {
8✔
2028
      goto cleanup_root;
×
2029
    }
2030

2031
    if (pStmt->sql.pQuery->haveResultSet) {
8✔
2032
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
16✔
2033
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
2034
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
8✔
2035
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
8✔
2036
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
8✔
2037
    }
2038

2039
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
8✔
2040
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
8✔
2041
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
8✔
2042

2043
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
2044
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
2045
    // }
2046

2047
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2048

2049
    return TSDB_CODE_SUCCESS;
8✔
2050

2051
  cleanup_root:
×
2052
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
×
2053
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
×
2054
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
×
2055
      pStmt->sql.pQuery->pRoot = NULL;
×
2056
    }
2057
    STMT_ERR_RET(code);
×
2058
  }
2059

2060
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,142,129✔
2061
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2062
  }
2063

2064
  STableDataCxt** pDataBlock = NULL;
1,142,266✔
2065

2066
  if (pStmt->exec.pCurrBlock) {
1,142,266✔
2067
    pDataBlock = &pStmt->exec.pCurrBlock;
1,138,031✔
2068
  } else {
2069
    pDataBlock =
2070
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,247✔
2071
    if (NULL == pDataBlock) {
3,247✔
2072
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
2073
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2074
    }
2075
    pStmt->exec.pCurrBlock = *pDataBlock;
3,247✔
2076
    if (pStmt->sql.stbInterlaceMode) {
3,247✔
2077
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
3,225✔
2078
      (*pDataBlock)->pData->aCol = NULL;
3,225✔
2079
    }
2080
    if (colIdx < -1) {
3,247✔
2081
      pStmt->sql.bindRowFormat = true;
×
2082
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
2083
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
2084
    }
2085
  }
2086

2087
  int64_t startUs2 = taosGetTimestampUs();
1,142,195✔
2088
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
1,142,195✔
2089

2090
  SStmtQNode* param = NULL;
1,142,104✔
2091
  if (pStmt->sql.stbInterlaceMode) {
1,141,826✔
2092
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
2,284,144✔
2093
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
2,284,564✔
2094
    taosArrayClear(param->tblData.aCol);
1,142,667✔
2095

2096
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2097

2098
    param->restoreTbCols = false;
1,141,886✔
2099
    param->tblData.isOrdered = true;
1,141,886✔
2100
    param->tblData.isDuplicateTs = false;
1,141,977✔
2101
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
1,141,977✔
2102

2103
    param->pCreateTbReq = pCreateTbReq;
1,141,652✔
2104
  }
2105

2106
  int64_t startUs3 = taosGetTimestampUs();
1,142,405✔
2107
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
1,142,405✔
2108

2109
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
1,142,266✔
2110
  SBlobSet* pBlob = NULL;
1,142,222✔
2111
  if (colIdx < 0) {
1,141,806✔
2112
    if (pStmt->sql.stbInterlaceMode) {
1,142,503✔
2113
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
1,142,199✔
2114
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
1,484,740✔
2115
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
1,142,567✔
2116
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
1,142,567✔
2117
      param->tblData.isOrdered = (*pDataBlock)->ordered;
1,142,439✔
2118
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
1,142,532✔
2119
    } else {
2120
      if (colIdx == -1) {
396✔
2121
        if (pStmt->sql.bindRowFormat) {
164✔
2122
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2123
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2124
        }
2125
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
164✔
2126
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
164✔
2127
      } else {
2128
        code =
2129
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
140✔
2130
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
140✔
2131
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
232✔
2132
      }
2133
    }
2134

2135
    if (code) {
1,142,466✔
2136
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
×
2137
      STMT_ERR_RET(code);
×
2138
    }
2139
  } else {
2140
    if (pStmt->sql.stbInterlaceMode) {
×
2141
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2142
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2143
    }
2144

2145
    if (pStmt->sql.bindRowFormat) {
×
2146
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2147
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2148
    }
2149

2150
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
2151
      STMT2_ELOG_E("bind column index not in sequence");
×
2152
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2153
    }
2154

2155
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2156

2157
    if (0 == colIdx) {
×
2158
      pStmt->bInfo.sBindRowNum = bind->num;
×
2159
    }
2160

2161
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
2162
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
×
2163
                                    pStmt->taos->optionInfo.charsetCxt);
×
2164
    if (code) {
×
2165
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
2166
      STMT_ERR_RET(code);
×
2167
    }
2168
  }
2169

2170
  int64_t startUs4 = taosGetTimestampUs();
1,142,770✔
2171
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
1,142,770✔
2172

2173
  if (pStmt->stbInterlaceMode) {
1,142,494✔
2174
    if (param) param->tblData.pBlobSet = pBlob;
1,142,769✔
2175
  }
2176

2177
  if (pStmt->sql.stbInterlaceMode) {
1,142,632✔
2178
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
1,142,834✔
2179
  } else {
2180
    STMT_ERR_RET(stmtAddBatch2(pStmt));
60✔
2181
  }
2182

2183
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
1,142,997✔
2184
  return TSDB_CODE_SUCCESS;
1,142,998✔
2185
}
2186

2187
/*
2188
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2189
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2190

2191
  int32_t code = 0;
2192
  int32_t finalCode = 0;
2193
  size_t  keyLen = 0;
2194
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2195
  while (pIter) {
2196
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2197
    char*          key = taosHashGetKey(pIter, &keyLen);
2198

2199
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2200
    if (pMeta->uid) {
2201
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2202
      continue;
2203
    }
2204

2205
    SSubmitBlkRsp* blkRsp = NULL;
2206
    int32_t        i = 0;
2207
    for (; i < pRsp->nBlocks; ++i) {
2208
      blkRsp = pRsp->pBlocks + i;
2209
      if (strlen(blkRsp->tblFName) != keyLen) {
2210
        continue;
2211
      }
2212

2213
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2214
        continue;
2215
      }
2216

2217
      break;
2218
    }
2219

2220
    if (i < pRsp->nBlocks) {
2221
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2222
               blkRsp->uid);
2223

2224
      pMeta->uid = blkRsp->uid;
2225
      pStmt->bInfo.tbUid = blkRsp->uid;
2226
    } else {
2227
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2228
      if (NULL == pStmt->pCatalog) {
2229
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2230
        if (code) {
2231
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2232
          finalCode = code;
2233
          continue;
2234
        }
2235
      }
2236

2237
      code = stmtCreateRequest(pStmt);
2238
      if (code) {
2239
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2240
        finalCode = code;
2241
        continue;
2242
      }
2243

2244
      STableMeta*      pTableMeta = NULL;
2245
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2246
                               .requestId = pStmt->exec.pRequest->requestId,
2247
                               .requestObjRefId = pStmt->exec.pRequest->self,
2248
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2249
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2250

2251
      pStmt->stat.ctgGetTbMetaNum++;
2252

2253
      taos_free_result(pStmt->exec.pRequest);
2254
      pStmt->exec.pRequest = NULL;
2255

2256
      if (code || NULL == pTableMeta) {
2257
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2258
        finalCode = code;
2259
        taosMemoryFree(pTableMeta);
2260
        continue;
2261
      }
2262

2263
      pMeta->uid = pTableMeta->uid;
2264
      pStmt->bInfo.tbUid = pTableMeta->uid;
2265
      taosMemoryFree(pTableMeta);
2266
    }
2267

2268
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2269
  }
2270

2271
  return finalCode;
2272
}
2273
*/
2274
/*
2275
int stmtStaticModeExec(TAOS_STMT* stmt) {
2276
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2277
  int32_t     code = 0;
2278
  SSubmitRsp* pRsp = NULL;
2279
  if (pStmt->sql.staticMode) {
2280
    return TSDB_CODE_TSC_STMT_API_ERROR;
2281
  }
2282

2283
  STMT_DLOG_E("start to exec");
2284

2285
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2286

2287
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2288
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2289

2290
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2291

2292
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2293
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2294
    if (code) {
2295
      pStmt->exec.pRequest->code = code;
2296
    } else {
2297
      tFreeSSubmitRsp(pRsp);
2298
      STMT_ERR_RET(stmtResetStmt(pStmt));
2299
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2300
    }
2301
  }
2302

2303
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2304

2305
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2306
  pStmt->affectedRows += pStmt->exec.affectedRows;
2307

2308
_return:
2309

2310
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2311

2312
  tFreeSSubmitRsp(pRsp);
2313

2314
  ++pStmt->sql.runTimes;
2315

2316
  STMT_RET(code);
2317
}
2318
*/
2319

2320
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
2321
  const STscObj* pTscObj = pRequest->pTscObj;
×
2322

2323
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
2324
  if (*pCxt == NULL) {
×
2325
    return terrno;
×
2326
  }
2327

2328
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
2329
                           .requestRid = pRequest->self,
×
2330
                           .acctId = pTscObj->acctId,
×
2331
                           .db = pRequest->pDb,
×
2332
                           .topicQuery = false,
2333
                           .pSql = pRequest->sqlstr,
×
2334
                           .sqlLen = pRequest->sqlLen,
×
2335
                           .pMsg = pRequest->msgBuf,
×
2336
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2337
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
2338
                           .pStmtCb = NULL,
2339
                           .pUser = pTscObj->user,
×
2340
                           .userId = pTscObj->userId,
×
2341
                           .pEffectiveUser = pRequest->effectiveUser,
×
2342
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
2343
                           .enableSysInfo = pTscObj->sysInfo,
×
2344
                           .async = true,
2345
                           .svrVer = pTscObj->sVer,
×
2346
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
2347
                           .allocatorId = pRequest->allocatorRefId,
×
2348
                           .parseSqlFp = clientParseSql,
2349
                           .parseSqlParam = pWrapper};
2350
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
2351
  (*pCxt)->biMode = biMode;
×
2352
  return TSDB_CODE_SUCCESS;
×
2353
}
2354

2355
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
2356
  STscStmt2*        pStmt = userdata;
×
2357
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
2358
  pStmt->asyncResultAvailable = true;
×
2359
  pStmt->exec.pRequest->inCallback = true;
×
2360

2361
  if (code == TSDB_CODE_SUCCESS) {
×
2362
    pStmt->exec.affectedRows = taos_affected_rows(res);
×
2363
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
2364
  }
2365

2366
  if (fp) {
×
2367
    fp(pStmt->options.userdata, res, code);
×
2368
  }
2369

2370
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
2371
    taosUsleep(1);
×
2372
  }
2373
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
2374
  ++pStmt->sql.runTimes;
×
2375
  if (pStmt->exec.pRequest != NULL) {
×
2376
    pStmt->exec.pRequest->inCallback = false;
×
2377
  }
2378

2379
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
2380
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2381
  }
2382
}
×
2383

2384
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
552,686✔
2385
  STscStmt2* pStmt = (STscStmt2*)stmt;
552,686✔
2386
  int32_t    code = 0;
552,686✔
2387
  int64_t    startUs = taosGetTimestampUs();
552,951✔
2388

2389
  STMT2_DLOG_E("start to exec");
552,951✔
2390

2391
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
552,965✔
2392
    return pStmt->errCode;
×
2393
  }
2394

2395
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
552,964✔
2396
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
553,038✔
2397
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2398
  }
2399
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
552,848✔
2400

2401
  if (pStmt->sql.stbInterlaceMode) {
552,914✔
2402
    STMT_ERR_RET(stmtAddBatch2(pStmt));
552,884✔
2403
  }
2404

2405
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
552,914✔
2406

2407
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
552,728✔
2408
    if (pStmt->sql.stbInterlaceMode) {
552,946✔
2409
      int64_t startTs = taosGetTimestampUs();
552,962✔
2410
      // wait for stmt bind thread to finish
2411
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
2,055,762✔
2412
        taosUsleep(1);
1,502,564✔
2413
      }
2414

2415
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
552,980✔
2416
        return pStmt->errCode;
×
2417
      }
2418

2419
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
552,994✔
2420
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
552,948✔
2421
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
552,963✔
2422
      pStmt->sql.siInfo.pVgroupHash = NULL;
552,842✔
2423
      pStmt->sql.siInfo.pVgroupList = NULL;
552,842✔
2424
    } else {
2425
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
46✔
2426
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
22✔
2427

2428
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
22✔
2429

2430
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
22✔
2431
    }
2432
  }
2433

2434
  pStmt->asyncResultAvailable = false;
552,784✔
2435
  SRequestObj*      pRequest = pStmt->exec.pRequest;
552,876✔
2436
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
552,922✔
2437
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
552,830✔
2438
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2439

2440
  if (!fp) {
552,922✔
2441
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
552,922✔
2442

2443
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
552,474✔
2444
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2445
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2446
      if (code) {
×
2447
        pStmt->exec.pRequest->code = code;
×
2448

2449
      } else {
2450
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2451
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2452
      }
2453
    }
2454

2455
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
552,974✔
2456

2457
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
552,882✔
2458
    if (affected_rows) {
552,841✔
2459
      *affected_rows = pStmt->exec.affectedRows;
552,736✔
2460
    }
2461
    pStmt->affectedRows += pStmt->exec.affectedRows;
552,611✔
2462

2463
    // wait for stmt bind thread to finish
2464
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
553,020✔
2465
      taosUsleep(1);
42✔
2466
    }
2467

2468
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
552,748✔
2469

2470
    ++pStmt->sql.runTimes;
552,595✔
2471
  } else {
2472
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
2473
    if (pWrapper == NULL) {
×
2474
      code = terrno;
×
2475
    } else {
2476
      pWrapper->pRequest = pRequest;
×
2477
      pRequest->pWrapper = pWrapper;
×
2478
    }
2479
    if (TSDB_CODE_SUCCESS == code) {
×
2480
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
2481
    }
2482
    pRequest->syncQuery = false;
×
2483
    pRequest->body.queryFp = asyncQueryCb;
×
2484
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
2485

2486
    pStmt->execSemWaited = false;
×
2487
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
2488
  }
2489

2490
_return:
552,687✔
2491
  if (code) {
552,687✔
2492
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2493
  }
2494
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
552,743✔
2495

2496
  STMT_RET(code);
552,881✔
2497
}
2498

2499
int stmtClose2(TAOS_STMT2* stmt) {
3,143✔
2500
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,143✔
2501

2502
  STMT2_DLOG_E("start to close stmt");
3,143✔
2503
  taosMemoryFreeClear(pStmt->db);
3,143✔
2504

2505
  if (pStmt->bindThreadInUse) {
3,143✔
2506
    // wait for stmt bind thread to finish
2507
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
3,113✔
2508
      taosUsleep(1);
×
2509
    }
2510

2511
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
3,113✔
2512
    pStmt->queue.stopQueue = true;
3,113✔
2513
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
3,113✔
2514
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
3,113✔
2515

2516
    (void)taosThreadJoin(pStmt->bindThread, NULL);
3,113✔
2517
    pStmt->bindThreadInUse = false;
3,113✔
2518

2519
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
3,113✔
2520
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
3,113✔
2521
  }
2522

2523
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
3,143✔
2524
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
3,143✔
2525
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2526
  }
2527
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
3,143✔
2528

2529
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
3,143✔
2530
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
3,143✔
2531

2532
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
3,143✔
2533
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2534
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2535
    }
2536
  }
2537

2538
  STMT2_DLOG("stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
3,143✔
2539
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2540
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2541
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2542
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2543
             pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2544
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2545
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2546
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2547
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2548
  if (pStmt->sql.stbInterlaceMode) {
3,143✔
2549
    pStmt->bInfo.tagsCached = false;
3,113✔
2550
  }
2551
  pStmt->bInfo.boundColsCached = false;
3,143✔
2552

2553
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
3,143✔
2554

2555
  if (pStmt->options.asyncExecFn) {
3,143✔
2556
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
2557
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2558
    }
2559
  }
2560
  taosMemoryFree(stmt);
3,143✔
2561

2562
  return TSDB_CODE_SUCCESS;
3,143✔
2563
}
2564

2565
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
2566
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2567

2568
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
2569
    return (char*)tstrerror(terrno);
×
2570
  }
2571

2572
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2573
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
×
2574
    pStmt->exec.pRequest->code = terrno;
×
2575
  }
2576

2577
  SRequestObj* pRequest = pStmt->exec.pRequest;
×
2578
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
×
2579
    return pRequest->msgBuf;
×
2580
  }
2581
  return (const char*)tstrerror(pRequest->code);
×
2582
}
2583
/*
2584
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2585

2586
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2587
*/
2588

2589
int stmtParseColFields2(TAOS_STMT2* stmt) {
22✔
2590
  int32_t    code = 0;
22✔
2591
  STscStmt2* pStmt = (STscStmt2*)stmt;
22✔
2592
  int32_t    preCode = pStmt->errCode;
22✔
2593

2594
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
22✔
2595
    return pStmt->errCode;
×
2596
  }
2597

2598
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
22✔
2599
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2600
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2601
  }
2602

2603
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
22✔
2604

2605
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
22✔
2606
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2607
    pStmt->bInfo.needParse = false;
×
2608
  }
2609
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
22✔
2610
    pStmt->bInfo.needParse = false;
×
2611
  }
2612

2613
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
22✔
2614

2615
  if (pStmt->bInfo.needParse) {
22✔
2616
    STMT_ERRI_JRET(stmtParseSql(pStmt));
22✔
2617
  }
2618

2619
_return:
22✔
2620
  // compatible with previous versions
2621
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
22✔
2622
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2623
  }
2624

2625
  pStmt->errCode = preCode;
22✔
2626

2627
  return code;
22✔
2628
}
2629

2630
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
22✔
2631
  int32_t code = stmtParseColFields2(stmt);
22✔
2632
  if (code != TSDB_CODE_SUCCESS) {
22✔
2633
    return code;
×
2634
  }
2635

2636
  return stmtFetchStbColFields2(stmt, nums, fields);
22✔
2637
}
2638

2639
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2640
  int32_t    code = 0;
×
2641
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2642
  int32_t    preCode = pStmt->errCode;
×
2643

2644
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2645
    return pStmt->errCode;
×
2646
  }
2647

2648
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2649

2650
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2651
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2652
    pStmt->bInfo.needParse = false;
×
2653
  }
2654

2655
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2656
    resetRequest(pStmt);
×
2657
  }
2658

2659
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2660

2661
  if (pStmt->bInfo.needParse) {
×
2662
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2663
  }
2664

2665
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2666
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2667
  } else {
2668
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2669
  }
2670

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

2673
_return:
×
2674

2675
  pStmt->errCode = preCode;
×
2676

2677
  return code;
×
2678
}
2679

2680
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
8✔
2681
  STscStmt2* pStmt = (STscStmt2*)stmt;
8✔
2682

2683
  STMT2_TLOG_E("start to use result");
8✔
2684

2685
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
8✔
2686
    STMT2_ELOG_E("useResult only for query statement");
×
2687
    return NULL;
×
2688
  }
2689

2690
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
8✔
2691
    STMT2_ELOG_E("use result after callBackFn return");
×
2692
    return NULL;
×
2693
  }
2694

2695
  return pStmt->exec.pRequest;
8✔
2696
}
2697

2698
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2699
  qInfo("async stmt bind thread started");
×
2700

2701
  ThreadArgs* targs = (ThreadArgs*)args;
×
2702
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2703

2704
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2705
  targs->fp(targs->param, NULL, code);
×
2706
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2707
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2708
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2709
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2710
  taosMemoryFree(args);
×
2711

2712
  qInfo("async stmt bind thread stopped");
×
2713

2714
  return code;
×
2715
}
2716

2717
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
×
2718
  if (pStmt == NULL || msg == NULL) {
×
2719
    return;
×
2720
  }
2721

2722
  if (pStmt->exec.pRequest == NULL) {
×
2723
    return;
×
2724
  }
2725

2726
  if (pStmt->exec.pRequest->msgBuf == NULL) {
×
2727
    return;
×
2728
  }
2729

2730
  size_t msgLen = strlen(msg);
×
2731
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
×
2732

2733
  if (msgLen >= bufLen) {
×
2734
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2735
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
2736
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2737
  } else {
2738
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
×
2739
    pStmt->exec.pRequest->msgBufLen = msgLen;
×
2740
  }
2741

2742
  return;
×
2743
}
2744

2745
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
×
2746
  stmtBuildErrorMsg(pStmt, msg);
×
2747
  pStmt->errCode = errorCode;
×
2748

2749
  return errorCode;
×
2750
}
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