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

taosdata / TDengine / #4879

11 Dec 2025 02:43AM UTC coverage: 64.544% (-0.03%) from 64.569%
#4879

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

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

3883 existing lines in 125 files now uncovered.

163565 of 253417 relevant lines covered (64.54%)

105600506.39 hits per line

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

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

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

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

11
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
12
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
23,537,429✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
23,538,821✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
23,538,271✔
UNCOV
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
16
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
17
    if (NULL == pTblBuf->pCurBuff) {
×
18
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
19
    }
20
    *pBuf = pTblBuf->pCurBuff;
×
21
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
22
  } else {
23
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
24
    if (NULL == buff) {
×
25
      return terrno;
×
26
    }
27

28
    if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
×
29
      return terrno;
×
30
    }
31

32
    pTblBuf->buffIdx++;
×
33
    pTblBuf->pCurBuff = buff;
×
34
    *pBuf = buff;
×
35
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
36
  }
37

38
  return TSDB_CODE_SUCCESS;
23,537,190✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
23,541,199✔
42
  int i = 0;
23,541,199✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
94,414,312✔
44
    if (pStmt->queue.stopQueue) {
70,898,422✔
45
      return false;
20,813✔
46
    }
47
    if (i < 10) {
70,878,434✔
48
      taosUsleep(1);
67,238,314✔
49
      i++;
67,230,488✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
3,640,120✔
52
      if (pStmt->queue.stopQueue) {
3,642,811✔
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
3,642,811✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
3,639,757✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
3,642,034✔
60
    }
61
  }
62

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

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

75
  SStmtQNode* node = pStmt->queue.head->next;
23,518,717✔
76
  pStmt->queue.head->next = node->next;
23,520,092✔
77
  if (pStmt->queue.tail == node) {
23,520,092✔
78
    pStmt->queue.tail = pStmt->queue.head;
11,940,132✔
79
  }
80
  node->next = NULL;
23,519,817✔
81
  *param = node;
23,519,817✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
23,520,092✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
23,521,281✔
85

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

88
  return true;
23,520,938✔
89
}
90

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

97
  param->next = NULL;
23,516,057✔
98

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

101
  pStmt->queue.tail->next = param;
23,519,646✔
102
  pStmt->queue.tail = param;
23,519,383✔
103
  pStmt->stat.bindDataNum++;
23,519,383✔
104

105
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
23,518,558✔
106
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
23,521,280✔
107

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

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

114
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
28,194,259✔
115
  int32_t code = 0;
28,194,259✔
116

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

136
  return code;
28,194,075✔
137
}
138

139
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
59,025,714✔
140
  int32_t code = 0;
59,025,714✔
141

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
59,025,714✔
143
    STMT2_LOG_SEQ(newStatus);
59,034,684✔
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
59,039,156✔
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) {
59,038,881✔
152
    case STMT_PREPARE:
21,911✔
153
      pStmt->errCode = 0;
21,911✔
154
      break;
19,706✔
155
    case STMT_SETTBNAME:
16,161,811✔
156
      if (STMT_STATUS_EQ(INIT)) {
16,161,811✔
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
16,162,086✔
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
16,160,998✔
163
    case STMT_SETTAGS:
11,968,716✔
164
      if (STMT_STATUS_EQ(INIT)) {
11,968,716✔
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
11,968,716✔
168
    case STMT_FETCH_FIELDS:
691✔
169
      if (STMT_STATUS_EQ(INIT)) {
691✔
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
691✔
173
    case STMT_BIND:
16,160,529✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
16,160,529✔
175
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
176
      }
177
      /*
178
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
179
              code = TSDB_CODE_TSC_STMT_API_ERROR;
180
            }
181
      */
182
      break;
16,161,091✔
183
    case STMT_BIND_COL:
×
184
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
185
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
186
      }
187
      break;
×
188
    case STMT_ADD_BATCH:
7,364,519✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
7,364,519✔
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
7,364,794✔
193
    case STMT_EXECUTE:
7,360,704✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
7,360,704✔
195
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
244✔
196
            STMT_STATUS_NE(BIND_COL)) {
×
197
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
198
        }
199
      } else {
200
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
7,360,197✔
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
202
        }
203
      }
204
      break;
7,360,166✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
59,036,162✔
211

212
  pStmt->sql.status = newStatus;
59,036,162✔
213

214
  return TSDB_CODE_SUCCESS;
59,036,999✔
215
}
216

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

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
22,095✔
221

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

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

229
  return TSDB_CODE_SUCCESS;
21,667✔
230
}
231

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

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

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

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

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

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

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

285
  return TSDB_CODE_SUCCESS;
22,095✔
286
}
287

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

291
  pStmt->sql.pVgHash = pVgHash;
22,095✔
292
  pStmt->exec.pBlockHash = pBlockHash;
22,095✔
293

294
  return TSDB_CODE_SUCCESS;
22,077✔
295
}
296

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

302
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
22,095✔
303
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
21,814✔
304

305
  pStmt->sql.autoCreateTbl = autoCreateTbl;
22,095✔
306

307
  return TSDB_CODE_SUCCESS;
22,358✔
308
}
309

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

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

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

319
  return TSDB_CODE_SUCCESS;
691✔
320
}
321

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

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

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

335
  pStmt->stat.parseSqlNum++;
22,602✔
336

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

340
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
22,339✔
341

342
  pStmt->bInfo.needParse = false;
22,602✔
343

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

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

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

402
  return TSDB_CODE_SUCCESS;
22,358✔
403
}
404

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

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

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

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

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

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

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

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

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

NEW
520
  return TSDB_CODE_SUCCESS;
×
521
}
522

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

531
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
7,402,299✔
532
  pStmt->bInfo.tbUid = 0;
7,402,299✔
533
  pStmt->bInfo.tbVgId = -1;
7,402,299✔
534
  pStmt->bInfo.tbType = 0;
7,402,024✔
535
  pStmt->bInfo.needParse = true;
7,402,024✔
536
  pStmt->bInfo.inExecCache = false;
7,402,299✔
537

538
  pStmt->bInfo.tbName[0] = 0;
7,402,299✔
539
  pStmt->bInfo.tbFName[0] = 0;
7,402,299✔
540
  if (!pStmt->bInfo.tagsCached) {
7,401,761✔
541
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
3,418,839✔
542
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
3,418,997✔
543
  }
544

545
  if (!pStmt->bInfo.boundColsCached) {
7,401,919✔
546
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
40,545✔
547
    pStmt->bInfo.boundCols = NULL;
40,545✔
548
  }
549

550
  if (!pStmt->sql.autoCreateTbl) {
7,402,457✔
551
    pStmt->bInfo.stbFName[0] = 0;
3,415,691✔
552
    pStmt->bInfo.tbSuid = 0;
3,415,954✔
553
  }
554

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

558
  return TSDB_CODE_SUCCESS;
7,401,809✔
559
}
560

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

566
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
7,357,022✔
567
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
7,357,022✔
568
  if (NULL == pTblBuf->pCurBuff) {
7,359,433✔
569
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
1,553✔
UNCOV
570
    return;
×
571
  }
572
  pTblBuf->buffIdx = 1;
7,357,880✔
573
  pTblBuf->buffOffset = sizeof(*pQueue->head);
7,357,880✔
574

575
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
7,357,880✔
576
  pQueue->qRemainNum = 0;
7,357,617✔
577
  pQueue->head->next = NULL;
7,357,880✔
578
}
579

580
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
7,380,576✔
581
  if (pStmt->sql.stbInterlaceMode) {
7,380,576✔
582
    if (deepClean) {
7,379,441✔
583
      taosHashCleanup(pStmt->exec.pBlockHash);
20,813✔
584
      pStmt->exec.pBlockHash = NULL;
20,813✔
585

586
      if (NULL != pStmt->exec.pCurrBlock) {
20,813✔
587
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
20,813✔
588
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
20,813✔
589
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
20,813✔
590
        pStmt->exec.pCurrBlock = NULL;
20,813✔
591
      }
592
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
20,813✔
593
        resetRequest(pStmt);
20,813✔
594
      }
595
    } else {
596
      pStmt->sql.siInfo.pTableColsIdx = 0;
7,358,628✔
597
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
7,358,628✔
598
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
7,358,128✔
599
    }
600
    if (NULL != pStmt->exec.pRequest) {
7,381,256✔
601
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
7,360,443✔
602
    }
603
  } else {
604
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
1,148✔
605
      resetRequest(pStmt);
641✔
606
    }
607

608
    size_t keyLen = 0;
2,196✔
609
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2,196✔
610
    while (pIter) {
9,882✔
611
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
7,686✔
612
      char*          key = taosHashGetKey(pIter, &keyLen);
7,686✔
613
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
7,686✔
614

615
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
7,686✔
616
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
854✔
617
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
1,952✔
618

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

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

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

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

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

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

642
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
7,382,354✔
643
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
7,381,296✔
644
             keepTable, deepClean);
645

646
  return TSDB_CODE_SUCCESS;
7,381,454✔
647
}
648

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

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

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

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

674
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
19,927✔
675
  while (pIter) {
20,781✔
676
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
854✔
677

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

682
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
854✔
683
  }
684
  taosHashCleanup(pStmt->sql.pTableCache);
19,927✔
685
  pStmt->sql.pTableCache = NULL;
19,927✔
686

687
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
19,927✔
688
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
19,927✔
689

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

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

703
  STMT2_TLOG_E("end to free SQL info");
19,927✔
704

705
  return TSDB_CODE_SUCCESS;
19,927✔
706
}
707

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

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

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

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

732
  *vgId = vgInfo.vgId;
11,965,629✔
733

734
  return TSDB_CODE_SUCCESS;
11,965,629✔
735
}
736

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

745
  pStmt->stat.ctgGetTbMetaNum++;
17,352✔
746

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

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

UNCOV
756
    STMT_ERR_RET(code);
×
757
  }
758

759
  STMT_ERR_RET(code);
17,352✔
760

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

767
  taosMemoryFree(pTableMeta);
17,352✔
768

769
  return TSDB_CODE_SUCCESS;
17,089✔
770
}
771

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

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

779
  return TSDB_CODE_SUCCESS;
5,978✔
780
}
781

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

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

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

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

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

805
  if (NULL == pStmt->pCatalog) {
27,364✔
806
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
19,683✔
807
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
19,420✔
808
  }
809

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

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

819
    return TSDB_CODE_SUCCESS;
21,404✔
820
  }
821

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

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

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

836
      pStmt->exec.pCurrBlock = pNewBlock;
5,978✔
837

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

840
      return TSDB_CODE_SUCCESS;
5,978✔
841
    }
842

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

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

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

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

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

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

UNCOV
859
    return TSDB_CODE_SUCCESS;
×
860
  }
861

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

UNCOV
868
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
869
    }
870

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

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

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

UNCOV
881
    return TSDB_CODE_SUCCESS;
×
882
  }
883

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

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

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

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

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

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

UNCOV
906
    return TSDB_CODE_SUCCESS;
×
907
  }
908

UNCOV
909
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
910

UNCOV
911
  return TSDB_CODE_SUCCESS;
×
912
}
913

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

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

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

UNCOV
925
  return TSDB_CODE_SUCCESS;
×
926
}
927

928
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
23,517,941✔
929
  SStmtQNode* pParam = (SStmtQNode*)param;
23,517,941✔
930

931
  if (pParam->restoreTbCols) {
23,517,941✔
932
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
23,518,577✔
933
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
16,159,438✔
934
      *p = taosArrayInit(20, POINTER_BYTES);
16,159,438✔
935
      if (*p == NULL) {
16,158,758✔
936
        pStmt->errCode = terrno;
989✔
937
      }
938
    }
939
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
7,359,139✔
940
    STMT2_TLOG_E("restore pTableCols finished");
7,360,444✔
941
  } else {
942
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
16,157,958✔
943
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
944
    // taosMemoryFree(pParam->pTbData);
945
    if (code != TSDB_CODE_SUCCESS) {
16,155,733✔
UNCOV
946
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
UNCOV
947
      pStmt->errCode = code;
×
948
    }
949
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
16,155,733✔
950
  }
951
}
23,521,946✔
952

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

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

959
  while (true) {
23,521,541✔
960
    SStmtQNode* asyncParam = NULL;
23,542,354✔
961

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

970
    stmtAsyncOutput(pStmt, asyncParam);
23,518,409✔
971
  }
972

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

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

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

991
  pStmt->bindThreadInUse = true;
20,813✔
992

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

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

1003
  return TSDB_CODE_SUCCESS;
20,813✔
1004
}
1005

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

1011
  return TSDB_CODE_SUCCESS;
19,927✔
1012
}
1013

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

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

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

1034
  return TSDB_CODE_SUCCESS;
20,813✔
1035
}
1036

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

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

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

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

1063
  if (NULL != pOptions) {
20,016✔
1064
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
19,233✔
1065
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
19,233✔
1066
      pStmt->stbInterlaceMode = true;
18,829✔
1067
    }
1068

1069
    pStmt->reqid = pOptions->reqid;
19,233✔
1070
  }
1071

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

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

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

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

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

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

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

1132
  pStmt->execSemWaited = false;
19,927✔
1133

1134
  // STMT_LOG_SEQ(STMT_INIT);
1135

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

1139
  return pStmt;
19,927✔
1140
}
1141

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

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

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

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

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

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

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

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

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

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

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

1220
  return TSDB_CODE_SUCCESS;
1,984✔
1221
}
1222

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

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

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

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

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

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

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

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

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

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

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

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

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

1283
  resetRequest(pStmt);
1,984✔
1284

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

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

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

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

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

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

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

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

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

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

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

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

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

1347
  return TSDB_CODE_SUCCESS;
1,984✔
1348
}
1349

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

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

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

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

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

1372
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
21,911✔
1373

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

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

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

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

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

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

1423
  SArray* pTblCols = NULL;
20,813✔
1424
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
20,814,149✔
1425
    pTblCols = taosArrayInit(20, POINTER_BYTES);
20,793,336✔
1426
    if (NULL == pTblCols) {
20,777,939✔
UNCOV
1427
      return terrno;
×
1428
    }
1429

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

1435
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
20,813✔
1436

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

1440
  return TSDB_CODE_SUCCESS;
20,813✔
1441
}
1442

1443
bool stmt2IsInsert(TAOS_STMT2* stmt) {
16,181,209✔
1444
  STscStmt2* pStmt = (STscStmt2*)stmt;
16,181,209✔
1445
  if (pStmt->sql.type) {
16,181,209✔
1446
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
16,140,579✔
1447
  }
1448

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

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

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

1461
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
16,158,411✔
1462
  STscStmt2* pStmt = (STscStmt2*)stmt;
16,158,411✔
1463

1464
  int64_t startUs = taosGetTimestampUs();
16,161,474✔
1465

1466
  STMT2_TLOG("start to set tbName:%s", tbName);
16,161,474✔
1467

1468
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
16,165,210✔
UNCOV
1469
    return pStmt->errCode;
×
1470
  }
1471

1472
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
16,165,222✔
1473

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

1480
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
16,161,418✔
1481
    STMT_ERR_RET(stmtCreateRequest(pStmt));
31,132✔
1482

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

1487
    STMT_ERR_RET(stmtGetFromCache(pStmt));
27,107✔
1488

1489
    if (pStmt->bInfo.needParse) {
27,382✔
1490
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
21,404✔
1491
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
21,124✔
1492

1493
      STMT_ERR_RET(stmtParseSql(pStmt));
21,667✔
1494
      if (!pStmt->sql.autoCreateTbl) {
21,667✔
1495
        uint64_t uid, suid;
16,117✔
1496
        int32_t  vgId;
16,117✔
1497
        int8_t   tableType;
16,117✔
1498

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

1506
  } else {
1507
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
16,129,736✔
1508
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
16,130,274✔
1509
    pStmt->exec.pRequest->requestId++;
16,130,274✔
1510
    pStmt->bInfo.needParse = false;
16,131,099✔
1511
  }
1512

1513
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
16,158,194✔
1514
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
20,813✔
1515
  }
1516

1517
  int64_t startUs2 = taosGetTimestampUs();
16,163,638✔
1518
  pStmt->stat.setTbNameUs += startUs2 - startUs;
16,163,638✔
1519

1520
  return TSDB_CODE_SUCCESS;
16,163,088✔
1521
}
1522

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

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

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

1535
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
9,983,937✔
1536

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

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

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

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

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

1578
  void* boundTags = NULL;
9,982,869✔
1579
  if (pStmt->sql.stbInterlaceMode) {
9,982,869✔
1580
    boundTags = pStmt->sql.siInfo.boundTags;
9,976,037✔
1581
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
9,976,037✔
1582
    if (NULL == pCreateTbReq) {
9,975,830✔
UNCOV
1583
      return terrno;
×
1584
    }
1585
    int32_t vgId = -1;
9,975,830✔
1586
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
9,975,830✔
1587
    (*pCreateTbReq)->uid = vgId;
9,979,733✔
1588
  } else {
1589
    boundTags = pStmt->bInfo.boundTags;
6,832✔
1590
  }
1591

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

1596
  return TSDB_CODE_SUCCESS;
9,984,816✔
1597
}
1598

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

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

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

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

1612
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
1,983,482✔
1613

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

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

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

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

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

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

1653
  if (pStmt->sql.fixValueTags) {
1,983,684✔
1654
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
1,981,046✔
1655
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
1,981,046✔
1656
    if ((*pCreateTbReq)->name) {
1,980,754✔
1657
      taosMemoryFree((*pCreateTbReq)->name);
1,981,272✔
1658
    }
1659
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
1,980,258✔
1660
    int32_t vgId = -1;
1,981,182✔
1661
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
1,981,182✔
1662
    (*pCreateTbReq)->uid = vgId;
1,981,430✔
1663
    return TSDB_CODE_SUCCESS;
1,981,430✔
1664
  }
1665

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

1674
  return TSDB_CODE_SUCCESS;
2,638✔
1675
}
1676

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

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

UNCOV
1687
  STableDataCxt** pDataBlock = NULL;
×
1688

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

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

UNCOV
1702
  return TSDB_CODE_SUCCESS;
×
1703
}
1704

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

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

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

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

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

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

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

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

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

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

1762
_return:
691✔
1763

1764
  pStmt->errCode = preCode;
691✔
1765

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

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

1780
    *idx = pStmt->exec.smInfo.pColIdx;
1781
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1782
  }
1783
}
1784
*/
1785
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
16,156,281✔
1786
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
16,156,281✔
1787
    pStmt->sql.siInfo.pVgroupHash =
7,359,590✔
1788
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
7,358,211✔
1789
  }
1790
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
16,158,473✔
1791
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
7,359,853✔
1792
  }
1793

1794
  if (NULL == pStmt->sql.siInfo.pRequest) {
16,158,763✔
1795
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
20,813✔
1796
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1797

1798
    if (pStmt->reqid != 0) {
20,550✔
1799
      pStmt->reqid++;
241✔
1800
    }
1801
    pStmt->exec.pRequest->syncQuery = true;
20,813✔
1802

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

1807
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
16,157,950✔
1808
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
126,209✔
1809
    pStmt->sql.siInfo.tbFromHash = true;
11,779✔
1810
  }
1811

1812
  if (0 == pStmt->sql.siInfo.firstName[0]) {
16,158,225✔
1813
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
18,566✔
1814
  }
1815

1816
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
16,158,488✔
1817
  param->next = NULL;
16,158,225✔
1818

1819
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
16,158,488✔
1820

1821
  if (pStmt->queue.stopQueue) {
16,161,097✔
1822
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
UNCOV
1823
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1824
  }
1825
  stmtEnqueue(pStmt, param);
16,160,834✔
1826

1827
  return TSDB_CODE_SUCCESS;
16,159,343✔
1828
}
1829

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

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

1850
  return TSDB_CODE_SUCCESS;
16,156,212✔
1851
}
1852

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

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

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

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

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

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

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

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

1891
  return TSDB_CODE_SUCCESS;
854✔
1892
}
1893

1894
static int stmtAddBatch2(TAOS_STMT2* stmt) {
7,365,405✔
1895
  STscStmt2* pStmt = (STscStmt2*)stmt;
7,365,405✔
1896

1897
  int64_t startUs = taosGetTimestampUs();
7,366,380✔
1898

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

1901
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,366,380✔
UNCOV
1902
    return pStmt->errCode;
×
1903
  }
1904

1905
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
7,366,105✔
1906

1907
  if (pStmt->sql.stbInterlaceMode) {
7,365,147✔
1908
    int64_t startUs2 = taosGetTimestampUs();
7,359,287✔
1909
    pStmt->stat.addBatchUs += startUs2 - startUs;
7,359,287✔
1910

1911
    pStmt->sql.siInfo.tableColsReady = false;
7,359,562✔
1912

1913
    SStmtQNode* param = NULL;
7,359,562✔
1914
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
14,718,052✔
1915
    param->restoreTbCols = true;
7,359,040✔
1916
    param->next = NULL;
7,359,040✔
1917

1918
    if (pStmt->sql.autoCreateTbl) {
7,359,315✔
1919
      pStmt->bInfo.tagsCached = true;
3,980,222✔
1920
    }
1921
    pStmt->bInfo.boundColsCached = true;
7,359,028✔
1922

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

1928
    stmtEnqueue(pStmt, param);
7,359,040✔
1929

1930
    return TSDB_CODE_SUCCESS;
7,359,489✔
1931
  }
1932

1933
  STMT_ERR_RET(stmtCacheBlock(pStmt));
6,832✔
1934

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

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

1952
  return TSDB_CODE_SUCCESS;
1953
}
1954

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

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

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

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

1978
  return TSDB_CODE_SUCCESS;
1979
}
1980
*/
1981

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

1986
  int64_t startUs = taosGetTimestampUs();
16,163,603✔
1987

1988
  if (qDebugFlag & DEBUG_TRACE) {
16,163,603✔
NEW
1989
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1990
  }
1991

1992
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
16,160,049✔
UNCOV
1993
    return pStmt->errCode;
×
1994
  }
1995

1996
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
16,160,850✔
1997

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

2003
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
16,161,726✔
UNCOV
2004
    resetRequest(pStmt);
×
2005
  }
2006

2007
  STMT_ERR_RET(stmtCreateRequest(pStmt));
16,162,001✔
2008
  if (pStmt->bInfo.needParse) {
16,160,141✔
UNCOV
2009
    code = stmtParseSql(pStmt);
×
UNCOV
2010
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2011
      goto cleanup_root;
×
2012
    }
2013
  }
2014

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

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

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

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

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

2060
    return TSDB_CODE_SUCCESS;
244✔
2061

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

2071
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
16,160,172✔
UNCOV
2072
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2073
  }
2074

2075
  STableDataCxt** pDataBlock = NULL;
16,159,137✔
2076

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

2098
  int64_t startUs2 = taosGetTimestampUs();
16,159,643✔
2099
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
16,159,643✔
2100

2101
  SStmtQNode* param = NULL;
16,157,742✔
2102
  if (pStmt->sql.stbInterlaceMode) {
16,158,017✔
2103
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
32,314,348✔
2104
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
32,314,080✔
2105
    taosArrayClear(param->tblData.aCol);
16,156,212✔
2106

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

2109
    param->restoreTbCols = false;
16,146,983✔
2110
    param->tblData.isOrdered = true;
16,146,983✔
2111
    param->tblData.isDuplicateTs = false;
16,146,983✔
2112
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
16,146,990✔
2113

2114
    param->pCreateTbReq = pCreateTbReq;
16,147,516✔
2115
  }
2116

2117
  int64_t startUs3 = taosGetTimestampUs();
16,159,045✔
2118
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
16,159,045✔
2119

2120
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
16,157,950✔
2121
  SBlobSet* pBlob = NULL;
16,157,950✔
2122
  if (colIdx < 0) {
16,157,950✔
2123
    if (pStmt->sql.stbInterlaceMode) {
16,160,543✔
2124
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
16,154,481✔
2125
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
18,158,683✔
2126
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
16,155,007✔
2127
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
16,154,744✔
2128
      param->tblData.isOrdered = (*pDataBlock)->ordered;
16,158,467✔
2129
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
16,157,642✔
2130
    } else {
2131
      if (colIdx == -1) {
6,879✔
2132
        if (pStmt->sql.bindRowFormat) {
6,832✔
2133
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2134
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2135
        }
2136
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
6,832✔
2137
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
6,832✔
2138
      } else {
2139
        code =
2140
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
179✔
2141
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
179✔
2142
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
179✔
2143
      }
2144
    }
2145

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

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

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

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

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

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

2181
  int64_t startUs4 = taosGetTimestampUs();
16,164,931✔
2182
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
16,164,931✔
2183

2184
  if (pStmt->stbInterlaceMode) {
16,164,118✔
2185
    if (param) param->tblData.pBlobSet = pBlob;
16,159,168✔
2186
  }
2187

2188
  if (pStmt->sql.stbInterlaceMode) {
16,164,680✔
2189
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
16,158,485✔
2190
  } else {
2191
    STMT_ERR_RET(stmtAddBatch2(pStmt));
6,195✔
2192
  }
2193

2194
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
16,165,863✔
2195
  return TSDB_CODE_SUCCESS;
16,166,138✔
2196
}
2197

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

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

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

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

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

2228
      break;
2229
    }
2230

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

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

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

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

2262
      pStmt->stat.ctgGetTbMetaNum++;
2263

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

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

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

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

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

2294
  STMT_DLOG_E("start to exec");
2295

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

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

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

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

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

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

2319
_return:
2320

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

2323
  tFreeSSubmitRsp(pRsp);
2324

2325
  ++pStmt->sql.runTimes;
2326

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

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

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

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

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

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

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

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

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

2394
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
7,358,573✔
2395
  STscStmt2* pStmt = (STscStmt2*)stmt;
7,358,573✔
2396
  int32_t    code = 0;
7,358,573✔
2397
  int64_t    startUs = taosGetTimestampUs();
7,361,047✔
2398

2399
  STMT2_DLOG_E("start to exec");
7,361,047✔
2400

2401
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,361,453✔
UNCOV
2402
    return pStmt->errCode;
×
2403
  }
2404

2405
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
7,360,640✔
2406
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
7,360,845✔
UNCOV
2407
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2408
  }
2409
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
7,360,148✔
2410

2411
  if (pStmt->sql.stbInterlaceMode) {
7,361,004✔
2412
    STMT_ERR_RET(stmtAddBatch2(pStmt));
7,360,169✔
2413
  }
2414

2415
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
7,360,033✔
2416

2417
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
7,359,035✔
2418
    if (pStmt->sql.stbInterlaceMode) {
7,358,747✔
2419
      int64_t startTs = taosGetTimestampUs();
7,359,527✔
2420
      // wait for stmt bind thread to finish
2421
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
28,503,866✔
2422
        taosUsleep(1);
21,142,078✔
2423
      }
2424

2425
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7,359,674✔
UNCOV
2426
        return pStmt->errCode;
×
2427
      }
2428

2429
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
7,359,921✔
2430
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
7,360,196✔
2431
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
7,359,921✔
2432
      pStmt->sql.siInfo.pVgroupHash = NULL;
7,359,014✔
2433
      pStmt->sql.siInfo.pVgroupList = NULL;
7,359,289✔
2434
    } else {
2435
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
619✔
2436
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
854✔
2437

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

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

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

2450
  if (!fp) {
7,360,181✔
2451
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
7,360,181✔
2452

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

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

2465
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
7,359,118✔
2466

2467
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
7,359,118✔
2468
    if (affected_rows) {
7,361,384✔
2469
      *affected_rows = pStmt->exec.affectedRows;
7,359,604✔
2470
    }
2471
    pStmt->affectedRows += pStmt->exec.affectedRows;
7,360,846✔
2472

2473
    // wait for stmt bind thread to finish
2474
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
7,364,012✔
2475
      taosUsleep(1);
2,628✔
2476
    }
2477

2478
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
7,359,782✔
2479

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

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

2500
_return:
7,359,679✔
2501
  if (code) {
7,359,679✔
2502
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2503
  }
2504
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
7,358,952✔
2505

2506
  STMT_RET(code);
7,358,952✔
2507
}
2508

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

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

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

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

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

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

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

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

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

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

2563
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
19,927✔
2564

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

2572
  return TSDB_CODE_SUCCESS;
19,927✔
2573
}
2574

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

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

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

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

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

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

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

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

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

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

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

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

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

2635
  pStmt->errCode = preCode;
691✔
2636

2637
  return code;
691✔
2638
}
2639

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

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

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

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

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

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

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

UNCOV
2669
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2670

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

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

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

UNCOV
2683
_return:
×
2684

UNCOV
2685
  pStmt->errCode = preCode;
×
2686

UNCOV
2687
  return code;
×
2688
}
2689

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

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

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

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

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

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

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

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

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

UNCOV
2724
  return code;
×
2725
}
2726

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

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

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

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

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

UNCOV
2752
  return;
×
2753
}
2754

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

UNCOV
2759
  return errorCode;
×
2760
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc