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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

62.4
/source/client/src/clientStmt2.c
1
#include "clientInt.h"
2
#include "clientLog.h"
3
#include "tdef.h"
4

5
#include "clientStmt.h"
6
#include "clientStmt2.h"
7

8
char* gStmt2StatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
9
                           "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
10

11
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
12
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
1,451,224✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,451,241✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,451,126✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
24✔
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,451,048✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,451,189✔
42
  int i = 0;
1,451,189✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
6,034,335✔
44
    if (pStmt->queue.stopQueue) {
4,586,276✔
45
      return false;
2,646✔
46
    }
47
    if (i < 10) {
4,583,672✔
48
      taosUsleep(1);
4,306,622✔
49
      i++;
4,305,986✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
277,050✔
52
      if (pStmt->queue.stopQueue) {
277,240✔
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
277,240✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
277,177✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
277,148✔
60
    }
61
  }
62

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

67
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,448,449✔
68
  if (pStmt->queue.head == pStmt->queue.tail) {
1,448,836✔
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,448,678✔
76
  pStmt->queue.head->next = node->next;
1,448,797✔
77
  if (pStmt->queue.tail == node) {
1,448,836✔
78
    pStmt->queue.tail = pStmt->queue.head;
787,903✔
79
  }
80
  node->next = NULL;
1,448,836✔
81
  *param = node;
1,448,758✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,448,715✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,448,773✔
85

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

88
  return true;
1,448,591✔
89
}
90

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

97
  param->next = NULL;
1,448,380✔
98

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

101
  pStmt->queue.tail->next = param;
1,448,607✔
102
  pStmt->queue.tail = param;
1,448,685✔
103
  pStmt->stat.bindDataNum++;
1,448,689✔
104

105
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,448,570✔
106
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,448,622✔
107

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

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

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

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

136
  return code;
2,540,848✔
137
}
138

139
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
3,478,288✔
140
  int32_t code = 0;
3,478,288✔
141

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
3,478,288✔
143
    STMT2_LOG_SEQ(newStatus);
3,480,523✔
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
3,478,750✔
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) {
3,480,148✔
152
    case STMT_PREPARE:
2,672✔
153
      pStmt->errCode = 0;
2,672✔
154
      break;
2,477✔
155
    case STMT_SETTBNAME:
977,486✔
156
      if (STMT_STATUS_EQ(INIT)) {
977,486✔
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
977,484✔
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
977,486✔
163
    case STMT_SETTAGS:
580,764✔
164
      if (STMT_STATUS_EQ(INIT)) {
580,764✔
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
580,764✔
168
    case STMT_FETCH_FIELDS:
18✔
169
      if (STMT_STATUS_EQ(INIT)) {
18✔
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
18✔
173
    case STMT_BIND:
977,351✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
977,351✔
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;
977,632✔
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:
470,940✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
470,940✔
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
470,940✔
193
    case STMT_EXECUTE:
470,917✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
470,917✔
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)) {
470,866✔
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
202
        }
203
      }
204
      break;
470,913✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
3,480,230✔
211

212
  pStmt->sql.status = newStatus;
3,480,230✔
213

214
  return TSDB_CODE_SUCCESS;
3,480,425✔
215
}
216

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

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
2,682✔
221

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

227
  *tbName = pStmt->bInfo.tbName;
2,664✔
228

229
  return TSDB_CODE_SUCCESS;
2,664✔
230
}
231

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

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

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

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

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

259
  if (cols) {
2,678✔
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++) {
×
UNCOV
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;
2,678✔
275
  }
276
  pStmt->bInfo.boundTags = tags;
2,641✔
277
  pStmt->bInfo.tagsCached = false;
2,641✔
278
  pStmt->bInfo.tbNameFlag = tbNameFlag;
2,682✔
279
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
2,639✔
280

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

285
  return TSDB_CODE_SUCCESS;
2,643✔
286
}
287

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

291
  pStmt->sql.pVgHash = pVgHash;
2,643✔
292
  pStmt->exec.pBlockHash = pBlockHash;
2,639✔
293

294
  return TSDB_CODE_SUCCESS;
2,598✔
295
}
296

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

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

305
  pStmt->sql.autoCreateTbl = autoCreateTbl;
2,639✔
306

307
  return TSDB_CODE_SUCCESS;
2,682✔
308
}
309

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

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

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

319
  return TSDB_CODE_SUCCESS;
18✔
320
}
321

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

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

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

335
  pStmt->stat.parseSqlNum++;
2,690✔
336

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

340
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
2,690✔
341

342
  pStmt->bInfo.needParse = false;
2,686✔
343

344
  if (pStmt->sql.type == 0) {
2,690✔
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) {
2,643✔
361
    pStmt->sql.stbInterlaceMode = false;
×
362
    return TSDB_CODE_SUCCESS;
×
363
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
2,639✔
364
    pStmt->sql.stbInterlaceMode = false;
×
365
  }
366

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

402
  return TSDB_CODE_SUCCESS;
2,682✔
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) {
2,786✔
529
  if (pStmt->exec.pRequest) {
2,786✔
530
    taos_free_result(pStmt->exec.pRequest);
2,672✔
531
    pStmt->exec.pRequest = NULL;
2,672✔
532
  }
533
  pStmt->asyncResultAvailable = false;
2,786✔
534
}
2,786✔
535

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

543
  pStmt->bInfo.tbName[0] = 0;
476,148✔
544
  pStmt->bInfo.tbFName[0] = 0;
476,148✔
545
  if (!pStmt->bInfo.tagsCached) {
476,068✔
546
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
282,941✔
547
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
283,048✔
548
  }
549

550
  if (!pStmt->bInfo.boundColsCached) {
476,133✔
551
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
5,170✔
552
    pStmt->bInfo.boundCols = NULL;
5,170✔
553
  }
554

555
  if (!pStmt->sql.autoCreateTbl) {
476,056✔
556
    pStmt->bInfo.stbFName[0] = 0;
282,936✔
557
    pStmt->bInfo.tbSuid = 0;
282,972✔
558
  }
559

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

563
  return TSDB_CODE_SUCCESS;
476,046✔
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) {
470,669✔
572
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
470,669✔
573
  if (NULL == pTblBuf->pCurBuff) {
470,853✔
574
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
192✔
575
    return;
×
576
  }
577
  pTblBuf->buffIdx = 1;
470,661✔
578
  pTblBuf->buffOffset = sizeof(*pQueue->head);
470,661✔
579

580
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
470,661✔
581
  pQueue->qRemainNum = 0;
470,583✔
582
  pQueue->head->next = NULL;
470,622✔
583
}
584

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

591
      if (NULL != pStmt->exec.pCurrBlock) {
2,646✔
592
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
2,646✔
593
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
2,646✔
594
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
2,646✔
595
        pStmt->exec.pCurrBlock = NULL;
2,646✔
596
      }
597
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
2,646✔
598
        resetRequest(pStmt);
2,646✔
599
      }
600
    } else {
601
      pStmt->sql.siInfo.pTableColsIdx = 0;
470,669✔
602
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
470,705✔
603
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
470,605✔
604
    }
605
    if (NULL != pStmt->exec.pRequest) {
473,576✔
606
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
470,930✔
607
    }
608
  } else {
609
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
83✔
610
      resetRequest(pStmt);
36✔
611
    }
612

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

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

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

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

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

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

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

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

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

651
  return TSDB_CODE_SUCCESS;
473,380✔
652
}
653

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

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

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

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

679
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
2,576✔
680
  while (pIter) {
2,594✔
681
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
18✔
682

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

687
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
18✔
688
  }
689
  taosHashCleanup(pStmt->sql.pTableCache);
2,576✔
690
  pStmt->sql.pTableCache = NULL;
2,576✔
691

692
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
2,576✔
693
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
2,576✔
694

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

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

708
  STMT2_TLOG_E("end to free SQL info");
2,576✔
709

710
  return TSDB_CODE_SUCCESS;
2,576✔
711
}
712

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

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

724
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
580,806✔
725
  if (TSDB_CODE_SUCCESS != code) {
580,818✔
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));
580,818✔
732
  if (TSDB_CODE_SUCCESS != code) {
580,866✔
733
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
192✔
734
    return code;
×
735
  }
736

737
  *vgId = vgInfo.vgId;
580,674✔
738

739
  return TSDB_CODE_SUCCESS;
580,674✔
740
}
741

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

750
  pStmt->stat.ctgGetTbMetaNum++;
2,482✔
751

752
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
2,482✔
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);
2,482✔
765

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

772
  taosMemoryFree(pTableMeta);
2,482✔
773

774
  return TSDB_CODE_SUCCESS;
2,482✔
775
}
776

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

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

784
  return TSDB_CODE_SUCCESS;
106✔
785
}
786

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

794
  pStmt->bInfo.needParse = true;
2,731✔
795
  pStmt->bInfo.inExecCache = false;
2,731✔
796

797
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,731✔
798
  if (pCxtInExec) {
2,770✔
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) {
2,770✔
811
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
2,529✔
812
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
2,529✔
813
  }
814

815
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
2,731✔
816
    if (pStmt->bInfo.inExecCache) {
2,664✔
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);
2,664✔
823

824
    return TSDB_CODE_SUCCESS;
2,625✔
825
  }
826

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

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

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

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

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

845
      return TSDB_CODE_SUCCESS;
106✔
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,448,447✔
934
  SStmtQNode* pParam = (SStmtQNode*)param;
1,448,447✔
935

936
  if (pParam->restoreTbCols) {
1,448,447✔
937
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,448,581✔
938
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
977,738✔
939
      *p = taosArrayInit(20, POINTER_BYTES);
977,738✔
940
      if (*p == NULL) {
977,675✔
941
        pStmt->errCode = terrno;
12✔
942
      }
943
    }
944
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
470,843✔
945
    STMT2_TLOG_E("restore pTableCols finished");
470,918✔
946
  } else {
947
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
977,570✔
948
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
949
    // taosMemoryFree(pParam->pTbData);
950
    if (code != TSDB_CODE_SUCCESS) {
977,580✔
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);
977,580✔
955
  }
956
}
1,448,761✔
957

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

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

964
  while (true) {
1,448,737✔
965
    SStmtQNode* asyncParam = NULL;
1,451,383✔
966

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

975
    stmtAsyncOutput(pStmt, asyncParam);
1,448,510✔
976
  }
977

978
  STMT2_DLOG_E("stmt2 bind thread stopped");
2,607✔
979
  return NULL;
2,646✔
980
}
981

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

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

996
  pStmt->bindThreadInUse = true;
2,646✔
997

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

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

1008
  return TSDB_CODE_SUCCESS;
2,646✔
1009
}
1010

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

1016
  return TSDB_CODE_SUCCESS;
2,576✔
1017
}
1018

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

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

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

1039
  return TSDB_CODE_SUCCESS;
2,646✔
1040
}
1041

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

1047
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
2,533✔
1048
  if (NULL == pStmt) {
2,576✔
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);
2,576✔
1054
  if (NULL == pStmt->sql.pTableCache) {
2,576✔
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;
2,576✔
1061
  if (taos->db[0] != '\0') {
2,576✔
1062
    pStmt->db = taosStrdup(taos->db);
2,576✔
1063
  }
1064
  pStmt->bInfo.needParse = true;
2,576✔
1065
  pStmt->sql.status = STMT_INIT;
2,576✔
1066
  pStmt->errCode = TSDB_CODE_SUCCESS;
2,576✔
1067

1068
  if (NULL != pOptions) {
2,576✔
1069
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
2,558✔
1070
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
2,558✔
1071
      pStmt->stbInterlaceMode = true;
2,550✔
1072
    }
1073

1074
    pStmt->reqid = pOptions->reqid;
2,558✔
1075
  }
1076

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

1083
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
2,550✔
1084
    if (NULL == pStmt->sql.siInfo.pTableHash) {
2,550✔
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));
2,550✔
1091
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
2,550✔
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);
2,550✔
1098
    if (NULL == pStmt->sql.siInfo.pTableCols) {
2,550✔
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);
2,550✔
1105
    if (TSDB_CODE_SUCCESS == code) {
2,550✔
1106
      code = stmtInitQueue(pStmt);
2,550✔
1107
    }
1108
    if (TSDB_CODE_SUCCESS == code) {
2,550✔
1109
      code = stmtStartBindThread(pStmt);
2,550✔
1110
    }
1111
    if (TSDB_CODE_SUCCESS != code) {
2,550✔
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;
2,576✔
1120
  if (pStmt->options.asyncExecFn) {
2,576✔
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);
2,576✔
1129
  if (TSDB_CODE_SUCCESS != code) {
2,576✔
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;
2,576✔
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,
2,576✔
1142
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1143

1144
  return pStmt;
2,576✔
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) {
96✔
1176
  int32_t code = TSDB_CODE_SUCCESS;
96✔
1177

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

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

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

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

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

1206
  return TSDB_CODE_SUCCESS;
96✔
1207
}
1208

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

1214
  pStmt->errCode = 0;
96✔
1215
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
96✔
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) {
96✔
1223
    if (pStmt->bindThreadInUse) {
96✔
1224
      while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
96✔
1225
        taosUsleep(1);
×
1226
      }
1227
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
96✔
1228
      pStmt->queue.stopQueue = true;
96✔
1229
      (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
96✔
1230
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
96✔
1231

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

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

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

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

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

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

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

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

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

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

1273
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
96✔
1274
  while (pIter) {
96✔
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);
96✔
1284

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

1290
  resetRequest(pStmt);
96✔
1291

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

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

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

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

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

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

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

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

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

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

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

1345
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
96✔
1346
  if (NULL == pStmt->sql.pTableCache) {
96✔
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;
96✔
1352

1353
  return TSDB_CODE_SUCCESS;
96✔
1354
}
1355

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

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

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

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

1372
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,672✔
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));
2,672✔
1379

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

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

1405
      if (pStmt->sql.stbInterlaceMode) {
2,625✔
1406
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
2,568✔
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;
2,633✔
1417
}
1418

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

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

1429
  SArray* pTblCols = NULL;
2,646✔
1430
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
2,647,283✔
1431
    pTblCols = taosArrayInit(20, POINTER_BYTES);
2,644,637✔
1432
    if (NULL == pTblCols) {
2,642,175✔
1433
      return terrno;
×
1434
    }
1435

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

1441
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
2,646✔
1442

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

1446
  return TSDB_CODE_SUCCESS;
2,646✔
1447
}
1448

1449
bool stmt2IsInsert(TAOS_STMT2* stmt) {
980,166✔
1450
  STscStmt2* pStmt = (STscStmt2*)stmt;
980,166✔
1451
  if (pStmt->sql.type) {
980,166✔
1452
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
974,848✔
1453
  }
1454

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

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

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

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

1470
  int64_t startUs = taosGetTimestampUs();
977,540✔
1471

1472
  STMT2_TLOG("start to set tbName:%s", tbName);
977,540✔
1473

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

1478
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
977,632✔
1479

1480
  int32_t insert = 0;
977,597✔
1481
  if (!stmt2IsInsert(stmt)) {
977,597✔
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));
977,618✔
1487

1488
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
977,603✔
1489
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1490
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
977,279✔
1491
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
977,459✔
1492

1493
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
977,459✔
1494
    STMT_ERR_RET(stmtGetFromCache(pStmt));
3,014✔
1495

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

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

1510
  } else {
1511
    pStmt->exec.pRequest->requestId++;
974,447✔
1512
    pStmt->bInfo.needParse = false;
974,605✔
1513
  }
1514

1515
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
977,289✔
1516
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
2,646✔
1517
  }
1518

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

1522
  return TSDB_CODE_SUCCESS;
977,700✔
1523
}
1524

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

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

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

1537
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
484,860✔
1538

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

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

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

1554
  STableDataCxt** pDataBlock = NULL;
484,752✔
1555
  if (pStmt->exec.pCurrBlock) {
484,752✔
1556
    pDataBlock = &pStmt->exec.pCurrBlock;
484,690✔
1557
  } else {
1558
    pDataBlock =
1559
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
62✔
1560
    if (NULL == pDataBlock) {
62✔
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) {
484,752✔
1566
    return TSDB_CODE_SUCCESS;
×
1567
  }
1568

1569
  STMT2_TLOG_E("start to bind stmt tag values");
484,752✔
1570

1571
  void* boundTags = NULL;
484,692✔
1572
  if (pStmt->sql.stbInterlaceMode) {
484,692✔
1573
    boundTags = pStmt->sql.siInfo.boundTags;
484,568✔
1574
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
484,568✔
1575
    if (NULL == pCreateTbReq) {
484,664✔
1576
      return terrno;
×
1577
    }
1578
    int32_t vgId = -1;
484,664✔
1579
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
484,664✔
1580
    (*pCreateTbReq)->uid = vgId;
484,844✔
1581
  } else {
1582
    boundTags = pStmt->bInfo.boundTags;
124✔
1583
  }
1584

1585
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
484,968✔
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;
484,896✔
1590
}
1591

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

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

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

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

1605
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
95,964✔
1606

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

1613
  if (pStmt->bInfo.needParse) {
95,916✔
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) {
95,916✔
1622
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1623
  }
1624

1625
  STableDataCxt** pDataBlock = NULL;
95,952✔
1626
  if (pStmt->exec.pCurrBlock) {
95,952✔
1627
    pDataBlock = &pStmt->exec.pCurrBlock;
95,832✔
1628
  } else {
1629
    pDataBlock =
1630
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
120✔
1631
    if (NULL == pDataBlock) {
120✔
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);
36✔
1634
    }
1635
  }
1636

1637
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
95,988✔
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) {
95,988✔
1643
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
95,856✔
1644
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
95,856✔
1645
    if ((*pCreateTbReq)->name) {
95,856✔
1646
      taosMemoryFree((*pCreateTbReq)->name);
95,856✔
1647
    }
1648
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
95,844✔
1649
    int32_t vgId = -1;
95,820✔
1650
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
95,820✔
1651
    (*pCreateTbReq)->uid = vgId;
95,868✔
1652
    return TSDB_CODE_SUCCESS;
95,868✔
1653
  }
1654

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

1663
  return TSDB_CODE_SUCCESS;
132✔
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) {
18✔
1695
  int32_t code = 0;
18✔
1696
  int32_t preCode = pStmt->errCode;
18✔
1697

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

1702
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
18✔
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;
18✔
1708
  bool            cleanStb = false;
18✔
1709

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

1718
  if (NULL == pDataBlock || NULL == *pDataBlock) {
18✔
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;
18✔
1724
  pStmt->sql.placeholderOfCols = 0;
18✔
1725
  int32_t totalNum = 0;
18✔
1726
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.boundCols,
18✔
1727
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1728
                                        &pStmt->sql.placeholderOfCols));
1729

1730
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
18✔
1731
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
18✔
1732
    qDestroyStmtDataBlock(*pDataBlock);
18✔
1733
    *pDataBlock = NULL;
18✔
1734
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
18✔
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;
18✔
1739
    pStmt->bInfo.tagsCached = false;
18✔
1740
    pStmt->bInfo.sname = (SName){0};
18✔
1741
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
18✔
1742
  }
1743

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

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

1751
_return:
18✔
1752

1753
  pStmt->errCode = preCode;
18✔
1754

1755
  return code;
18✔
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) {
977,564✔
1775
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
977,564✔
1776
    pStmt->sql.siInfo.pVgroupHash =
470,780✔
1777
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
470,780✔
1778
  }
1779
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
977,798✔
1780
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
470,834✔
1781
  }
1782

1783
  if (NULL == pStmt->sql.siInfo.pRequest) {
977,666✔
1784
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
2,646✔
1785
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1786

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

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

1796
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
977,705✔
1797
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
18,030✔
1798
    pStmt->sql.siInfo.tbFromHash = true;
1,554✔
1799
  }
1800

1801
  if (0 == pStmt->sql.siInfo.firstName[0]) {
977,744✔
1802
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
2,550✔
1803
  }
1804

1805
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
977,744✔
1806
  param->next = NULL;
977,705✔
1807

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

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

1816
  return TSDB_CODE_SUCCESS;
977,706✔
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)) {
977,705✔
1822
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
977,628✔
1823
      break;
977,642✔
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;
977,642✔
1840
}
1841

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

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

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

1855
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
18✔
1856
  if (!pSrc) {
18✔
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;
18✔
1861

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

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

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

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

1880
  return TSDB_CODE_SUCCESS;
18✔
1881
}
1882

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

1886
  int64_t startUs = taosGetTimestampUs();
471,018✔
1887

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

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

1894
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
470,901✔
1895

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

1900
    pStmt->sql.siInfo.tableColsReady = false;
470,852✔
1901

1902
    SStmtQNode* param = NULL;
470,852✔
1903
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
941,703✔
1904
    param->restoreTbCols = true;
470,812✔
1905
    param->next = NULL;
470,855✔
1906

1907
    if (pStmt->sql.autoCreateTbl) {
470,890✔
1908
      pStmt->bInfo.tagsCached = true;
192,980✔
1909
    }
1910
    pStmt->bInfo.boundColsCached = true;
470,851✔
1911

1912
    if (pStmt->queue.stopQueue) {
470,890✔
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);
470,816✔
1918

1919
    return TSDB_CODE_SUCCESS;
470,879✔
1920
  }
1921

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

1924
  return TSDB_CODE_SUCCESS;
124✔
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) {
977,218✔
1972
  STscStmt2* pStmt = (STscStmt2*)stmt;
977,218✔
1973
  int32_t    code = 0;
977,218✔
1974

1975
  int64_t startUs = taosGetTimestampUs();
977,657✔
1976

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

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

1985
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
977,424✔
1986

1987
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
977,653✔
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) {
977,577✔
1993
    resetRequest(pStmt);
×
1994
  }
1995

1996
  STMT_ERR_RET(stmtCreateRequest(pStmt));
977,618✔
1997
  if (pStmt->bInfo.needParse) {
977,354✔
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) {
977,196✔
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) {
977,346✔
2061
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2062
  }
2063

2064
  STableDataCxt** pDataBlock = NULL;
977,236✔
2065

2066
  if (pStmt->exec.pCurrBlock) {
977,236✔
2067
    pDataBlock = &pStmt->exec.pCurrBlock;
974,851✔
2068
  } else {
2069
    pDataBlock =
2070
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,664✔
2071
    if (NULL == pDataBlock) {
2,664✔
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;
2,664✔
2076
    if (pStmt->sql.stbInterlaceMode) {
2,664✔
2077
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
2,646✔
2078
      (*pDataBlock)->pData->aCol = NULL;
2,646✔
2079
    }
2080
    if (colIdx < -1) {
2,664✔
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();
977,662✔
2088
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
977,662✔
2089

2090
  SStmtQNode* param = NULL;
977,539✔
2091
  if (pStmt->sql.stbInterlaceMode) {
977,461✔
2092
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,955,115✔
2093
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
1,955,148✔
2094
    taosArrayClear(param->tblData.aCol);
977,642✔
2095

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

2098
    param->restoreTbCols = false;
976,822✔
2099
    param->tblData.isOrdered = true;
976,822✔
2100
    param->tblData.isDuplicateTs = false;
976,945✔
2101
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
976,980✔
2102

2103
    param->pCreateTbReq = pCreateTbReq;
976,539✔
2104
  }
2105

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

2109
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
976,992✔
2110
  SBlobSet* pBlob = NULL;
977,195✔
2111
  if (colIdx < 0) {
977,039✔
2112
    if (pStmt->sql.stbInterlaceMode) {
977,292✔
2113
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
977,400✔
2114
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
1,269,150✔
2115
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
977,562✔
2116
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
977,521✔
2117
      param->tblData.isOrdered = (*pDataBlock)->ordered;
977,630✔
2118
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
977,669✔
2119
    } else {
2120
      if (colIdx == -1) {
76✔
2121
        if (pStmt->sql.bindRowFormat) {
124✔
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,
124✔
2126
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
124✔
2127
      } else {
2128
        code =
2129
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
×
2130
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
UNCOV
2131
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
×
2132
      }
2133
    }
2134

2135
    if (code) {
977,676✔
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) {
47✔
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();
977,727✔
2171
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
977,727✔
2172

2173
  if (pStmt->stbInterlaceMode) {
977,493✔
2174
    if (param) param->tblData.pBlobSet = pBlob;
977,828✔
2175
  }
2176

2177
  if (pStmt->sql.stbInterlaceMode) {
977,649✔
2178
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
977,696✔
2179
  } else {
UNCOV
2180
    STMT_ERR_RET(stmtAddBatch2(pStmt));
×
2181
  }
2182

2183
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
977,830✔
2184
  return TSDB_CODE_SUCCESS;
977,869✔
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) {
470,740✔
2385
  STscStmt2* pStmt = (STscStmt2*)stmt;
470,740✔
2386
  int32_t    code = 0;
470,740✔
2387
  int64_t    startUs = taosGetTimestampUs();
470,944✔
2388

2389
  STMT2_DLOG_E("start to exec");
470,944✔
2390

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

2395
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
470,944✔
2396
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
470,956✔
2397
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2398
  }
2399
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
470,869✔
2400

2401
  if (pStmt->sql.stbInterlaceMode) {
470,905✔
2402
    STMT_ERR_RET(stmtAddBatch2(pStmt));
470,891✔
2403
  }
2404

2405
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
470,905✔
2406

2407
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
470,850✔
2408
    if (pStmt->sql.stbInterlaceMode) {
470,893✔
2409
      int64_t startTs = taosGetTimestampUs();
470,918✔
2410
      // wait for stmt bind thread to finish
2411
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
2,134,502✔
2412
        taosUsleep(1);
1,663,646✔
2413
      }
2414

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

2419
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
470,894✔
2420
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
470,855✔
2421
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
470,801✔
2422
      pStmt->sql.siInfo.pVgroupHash = NULL;
470,714✔
2423
      pStmt->sql.siInfo.pVgroupList = NULL;
470,753✔
2424
    } else {
2425
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
18✔
2426
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
18✔
2427

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

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

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

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

2443
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
470,707✔
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);
470,956✔
2456

2457
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
470,803✔
2458
    if (affected_rows) {
470,920✔
2459
      *affected_rows = pStmt->exec.affectedRows;
470,752✔
2460
    }
2461
    pStmt->affectedRows += pStmt->exec.affectedRows;
470,648✔
2462

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

2468
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
470,686✔
2469

2470
    ++pStmt->sql.runTimes;
470,720✔
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:
470,722✔
2491
  if (code) {
470,722✔
2492
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2493
  }
2494
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
470,702✔
2495

2496
  STMT_RET(code);
470,778✔
2497
}
2498

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

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

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

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

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

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

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

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

2532
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
2,576✔
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
2,576✔
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) {
2,576✔
2549
    pStmt->bInfo.tagsCached = false;
2,550✔
2550
  }
2551
  pStmt->bInfo.boundColsCached = false;
2,576✔
2552

2553
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
2,576✔
2554

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

2562
  return TSDB_CODE_SUCCESS;
2,576✔
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) {
18✔
2590
  int32_t    code = 0;
18✔
2591
  STscStmt2* pStmt = (STscStmt2*)stmt;
18✔
2592
  int32_t    preCode = pStmt->errCode;
18✔
2593

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

2598
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
18✔
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));
18✔
2604

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

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

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

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

2625
  pStmt->errCode = preCode;
18✔
2626

2627
  return code;
18✔
2628
}
2629

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

2636
  return stmtFetchStbColFields2(stmt, nums, fields);
18✔
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