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

taosdata / TDengine / #4924

09 Jan 2026 08:13AM UTC coverage: 65.354% (-0.02%) from 65.373%
#4924

push

travis-ci

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

33 of 56 new or added lines in 8 files covered. (58.93%)

3192 existing lines in 116 files now uncovered.

198218 of 303297 relevant lines covered (65.35%)

118995160.34 hits per line

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

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

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

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

12

13
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
14
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
1,589,193✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,589,254✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,588,861✔
17
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
65✔
UNCOV
18
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
19
    if (NULL == pTblBuf->pCurBuff) {
×
20
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
21
    }
UNCOV
22
    *pBuf = pTblBuf->pCurBuff;
×
23
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
24
  } else {
UNCOV
25
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
26
    if (NULL == buff) {
×
27
      return terrno;
×
28
    }
29

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

UNCOV
34
    pTblBuf->buffIdx++;
×
35
    pTblBuf->pCurBuff = buff;
×
36
    *pBuf = buff;
×
37
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
38
  }
39

40
  return TSDB_CODE_SUCCESS;
1,589,030✔
41
}
42

43
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,589,704✔
44
  int i = 0;
1,589,704✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
6,826,771✔
46
    if (pStmt->queue.stopQueue) {
5,240,245✔
47
      return false;
3,130✔
48
    }
49
    if (i < 10) {
5,236,814✔
50
      taosUsleep(1);
4,920,576✔
51
      i++;
4,920,311✔
52
    } else {
53
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
316,238✔
54
      if (pStmt->queue.stopQueue) {
316,812✔
UNCOV
55
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
56
        return false;
×
57
      }
58
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
316,812✔
59
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
316,591✔
60
      }
61
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
316,786✔
62
    }
63
  }
64

65
  if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
1,586,039✔
UNCOV
66
    return false;
×
67
  }
68

69
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,586,039✔
70
  if (pStmt->queue.head == pStmt->queue.tail) {
1,586,518✔
UNCOV
71
    pStmt->queue.qRemainNum = 0;
×
72
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
73
    STMT2_ELOG_E("interlace queue is empty, cannot dequeue");
×
74
    return false;
×
75
  }
76

77
  SStmtQNode* node = pStmt->queue.head->next;
1,586,475✔
78
  pStmt->queue.head->next = node->next;
1,586,475✔
79
  if (pStmt->queue.tail == node) {
1,586,561✔
80
    pStmt->queue.tail = pStmt->queue.head;
838,767✔
81
  }
82
  node->next = NULL;
1,586,515✔
83
  *param = node;
1,586,472✔
84

85
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,586,515✔
86
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,586,570✔
87

88
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
1,586,587✔
89

90
  return true;
1,586,613✔
91
}
92

93
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
1,585,806✔
94
  if (param == NULL) {
1,585,806✔
UNCOV
95
    STMT2_ELOG_E("enqueue param is NULL");
×
96
    return;
×
97
  }
98

99
  param->next = NULL;
1,585,806✔
100

101
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,585,935✔
102

103
  pStmt->queue.tail->next = param;
1,586,194✔
104
  pStmt->queue.tail = param;
1,586,280✔
105
  pStmt->stat.bindDataNum++;
1,586,234✔
106

107
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,586,148✔
108
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,586,205✔
109

110
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,586,314✔
111

112
  STMT2_TLOG("enqueue param:%p, remainNum:%" PRId64 ", restoreTbCols:%d", param, pStmt->queue.qRemainNum,
1,586,379✔
113
             param->restoreTbCols);
114
}
115

116
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
2,773,681✔
117
  int32_t code = 0;
2,773,681✔
118

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

138
  return code;
2,773,737✔
139
}
140

141
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
3,803,681✔
142
  int32_t code = 0;
3,803,681✔
143

144
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
3,803,681✔
145
    STMT2_LOG_SEQ(newStatus);
3,804,383✔
146
  }
147

148
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
3,804,256✔
UNCOV
149
    STMT2_ELOG("stmt already failed with err:%s, please use stmt prepare", tstrerror(pStmt->errCode));
×
150
    return pStmt->errCode;
×
151
  }
152

153
  switch (newStatus) {
3,804,577✔
154
    case STMT_PREPARE:
3,103✔
155
      pStmt->errCode = 0;
3,103✔
156
      break;
2,532✔
157
    case STMT_SETTBNAME:
1,069,364✔
158
      if (STMT_STATUS_EQ(INIT)) {
1,069,364✔
UNCOV
159
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
160
      }
161
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
1,069,545✔
UNCOV
162
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
      }
164
      break;
1,069,588✔
165
    case STMT_SETTAGS:
629,331✔
166
      if (STMT_STATUS_EQ(INIT)) {
629,331✔
UNCOV
167
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
168
      }
169
      break;
629,331✔
170
    case STMT_FETCH_FIELDS:
23✔
171
      if (STMT_STATUS_EQ(INIT)) {
23✔
UNCOV
172
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
173
      }
174
      break;
23✔
175
    case STMT_BIND:
1,069,232✔
176
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
1,069,232✔
UNCOV
177
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
178
      }
179
      /*
180
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
181
              code = TSDB_CODE_TSC_STMT_API_ERROR;
182
            }
183
      */
184
      break;
1,069,444✔
UNCOV
185
    case STMT_BIND_COL:
×
186
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
187
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
188
      }
UNCOV
189
      break;
×
190
    case STMT_ADD_BATCH:
516,754✔
191
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
516,754✔
UNCOV
192
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
193
      }
194
      break;
516,840✔
195
    case STMT_EXECUTE:
516,770✔
196
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
516,770✔
197
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
8✔
UNCOV
198
            STMT_STATUS_NE(BIND_COL)) {
×
199
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
200
        }
201
      } else {
202
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
516,719✔
UNCOV
203
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
204
        }
205
      }
206
      break;
516,727✔
UNCOV
207
    default:
×
208
      code = TSDB_CODE_APP_ERROR;
×
209
      break;
×
210
  }
211

212
  STMT_ERR_RET(code);
3,804,485✔
213

214
  pStmt->sql.status = newStatus;
3,804,485✔
215

216
  return TSDB_CODE_SUCCESS;
3,804,758✔
217
}
218

219
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
3,178✔
220
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,178✔
221

222
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
3,178✔
223

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

229
  *tbName = pStmt->bInfo.tbName;
3,155✔
230

231
  return TSDB_CODE_SUCCESS;
3,155✔
232
}
233

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

243
  if ((tags != NULL && ((SBoundColInfo*)tags)->numOfCols == 0) || !autoCreateTbl) {
3,178✔
244
    pStmt->sql.autoCreateTbl = false;
2,953✔
245
  }
246

247
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
3,178✔
248
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
3,178✔
249
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
3,178✔
250

251
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
3,178✔
252
  pStmt->bInfo.tbSuid = pTableMeta->suid;
3,178✔
253
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
3,118✔
254
  pStmt->bInfo.tbType = pTableMeta->tableType;
3,118✔
255

256
  if (!pStmt->bInfo.tagsCached) {
3,178✔
257
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
3,118✔
258
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
3,178✔
259
  }
260

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

283
  if (pTableMeta->tableType != TSDB_CHILD_TABLE && pTableMeta->tableType != TSDB_SUPER_TABLE) {
3,118✔
UNCOV
284
    pStmt->sql.stbInterlaceMode = false;
×
285
  }
286

287
  return TSDB_CODE_SUCCESS;
3,178✔
288
}
289

290
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
3,118✔
291
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,118✔
292

293
  pStmt->sql.pVgHash = pVgHash;
3,118✔
294
  pStmt->exec.pBlockHash = pBlockHash;
3,118✔
295

296
  return TSDB_CODE_SUCCESS;
3,178✔
297
}
298

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

304
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
3,118✔
305
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
3,118✔
306

307
  pStmt->sql.autoCreateTbl = autoCreateTbl;
3,178✔
308

309
  return TSDB_CODE_SUCCESS;
3,178✔
310
}
311

312
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
23✔
313
  STscStmt2* pStmt = (STscStmt2*)stmt;
23✔
314

315
  *pVgHash = pStmt->sql.pVgHash;
23✔
316
  pStmt->sql.pVgHash = NULL;
23✔
317

318
  *pBlockHash = pStmt->exec.pBlockHash;
23✔
319
  pStmt->exec.pBlockHash = NULL;
23✔
320

321
  return TSDB_CODE_SUCCESS;
23✔
322
}
323

324
static int32_t stmtParseSql(STscStmt2* pStmt) {
3,142✔
325
  pStmt->exec.pCurrBlock = NULL;
3,142✔
326

327
  SStmtCallback stmtCb = {
3,186✔
328
      .pStmt = pStmt,
329
      .getTbNameFn = stmtGetTbName,
330
      .setInfoFn = stmtUpdateInfo,
331
      .getExecInfoFn = stmtGetExecInfo,
332
  };
333

334
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3,186✔
335
  pStmt->exec.pRequest->stmtBindVersion = 2;
3,186✔
336

337
  pStmt->stat.parseSqlNum++;
3,186✔
338

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

342
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
3,186✔
343

344
  pStmt->bInfo.needParse = false;
3,186✔
345

346
  if (pStmt->sql.type == 0) {
3,186✔
347
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
8✔
UNCOV
348
      pStmt->sql.type = STMT_TYPE_INSERT;
×
349
      pStmt->sql.stbInterlaceMode = false;
×
350
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
8✔
351
      pStmt->sql.type = STMT_TYPE_QUERY;
8✔
352
      pStmt->sql.stbInterlaceMode = false;
8✔
353

354
      return TSDB_CODE_SUCCESS;
8✔
355
    } else {
UNCOV
356
      STMT2_ELOG_E("only support select or insert sql");
×
357
      if (pStmt->exec.pRequest->msgBuf) {
×
358
        tstrncpy(pStmt->exec.pRequest->msgBuf, "stmt only support select or insert", pStmt->exec.pRequest->msgBufLen);
×
359
      }
UNCOV
360
      return TSDB_CODE_PAR_SYNTAX_ERROR;
×
361
    }
362
  } else if (pStmt->sql.type == STMT_TYPE_QUERY) {
3,118✔
UNCOV
363
    pStmt->sql.stbInterlaceMode = false;
×
364
    return TSDB_CODE_SUCCESS;
×
365
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
3,118✔
UNCOV
366
    pStmt->sql.stbInterlaceMode = false;
×
367
  }
368

369
  STableDataCxt** pSrc =
370
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,178✔
371
  if (NULL == pSrc || NULL == *pSrc) {
3,118✔
UNCOV
372
    STMT2_ELOG("fail to get exec.pBlockHash, maybe parse failed, tbFName:%s", pStmt->bInfo.tbFName);
×
373
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
374
  }
375

376
  STableDataCxt* pTableCtx = *pSrc;
3,118✔
377
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
3,118✔
UNCOV
378
    STMT2_TLOG("destroy pCreateTbReq for no-using insert, tbFName:%s", pStmt->bInfo.tbFName);
×
379
    tdDestroySVCreateTbReq(pTableCtx->pData->pCreateTbReq);
×
380
    taosMemoryFreeClear(pTableCtx->pData->pCreateTbReq);
×
381
    pTableCtx->pData->pCreateTbReq = NULL;
×
382
  }
383
  // if (pStmt->sql.stbInterlaceMode) {
384
  //   int16_t lastIdx = -1;
385

386
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
387
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
388
  //       pStmt->sql.stbInterlaceMode = false;
389
  //       break;
390
  //     }
391

392
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
393
  //   }
394
  // }
395

396
  if (NULL == pStmt->sql.pBindInfo) {
3,118✔
397
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
3,155✔
398
    if (NULL == pStmt->sql.pBindInfo) {
3,155✔
UNCOV
399
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
400
      return terrno;
×
401
    }
402
  }
403

404
  return TSDB_CODE_SUCCESS;
3,178✔
405
}
406

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

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

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

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

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

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

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

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

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

UNCOV
521
      if (!isNull && current_buf) {
×
522
        current_buf += len;
×
523
      }
524
    }
525
  }
526

UNCOV
527
  return TSDB_CODE_SUCCESS;
×
528
}
529

530
static void resetRequest(STscStmt2* pStmt) {
3,292✔
531
  if (pStmt->exec.pRequest) {
3,292✔
532
    taos_free_result(pStmt->exec.pRequest);
3,163✔
533
    pStmt->exec.pRequest = NULL;
3,163✔
534
  }
535
  pStmt->asyncResultAvailable = false;
3,292✔
536
}
3,292✔
537

538
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
522,854✔
539
  pStmt->bInfo.tbUid = 0;
522,854✔
540
  pStmt->bInfo.tbVgId = -1;
522,943✔
541
  pStmt->bInfo.tbType = 0;
522,854✔
542
  pStmt->bInfo.needParse = true;
522,943✔
543
  pStmt->bInfo.inExecCache = false;
522,943✔
544

545
  pStmt->bInfo.tbName[0] = 0;
522,854✔
546
  pStmt->bInfo.tbFName[0] = 0;
522,854✔
547
  if (!pStmt->bInfo.tagsCached) {
522,725✔
548
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
313,594✔
549
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
313,792✔
550
  }
551

552
  if (!pStmt->bInfo.boundColsCached) {
522,880✔
553
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
6,141✔
554
    pStmt->bInfo.boundCols = NULL;
6,141✔
555
  }
556

557
  if (!pStmt->sql.autoCreateTbl) {
522,969✔
558
    pStmt->bInfo.stbFName[0] = 0;
313,571✔
559
    pStmt->bInfo.tbSuid = 0;
313,666✔
560
  }
561

562
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
522,791✔
563
             pStmt->sql.autoCreateTbl);
564

565
  return TSDB_CODE_SUCCESS;
522,796✔
566
}
567

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

573
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
516,422✔
574
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
516,422✔
575
  if (NULL == pTblBuf->pCurBuff) {
516,750✔
576
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
247✔
UNCOV
577
    return;
×
578
  }
579
  pTblBuf->buffIdx = 1;
516,546✔
580
  pTblBuf->buffOffset = sizeof(*pQueue->head);
516,546✔
581

582
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
516,546✔
583
  pQueue->qRemainNum = 0;
516,371✔
584
  pQueue->head->next = NULL;
516,374✔
585
}
586

587
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
519,616✔
588
  if (pStmt->sql.stbInterlaceMode) {
519,616✔
589
    if (deepClean) {
519,720✔
590
      taosHashCleanup(pStmt->exec.pBlockHash);
3,130✔
591
      pStmt->exec.pBlockHash = NULL;
3,130✔
592

593
      if (NULL != pStmt->exec.pCurrBlock) {
3,130✔
594
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
3,130✔
595
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
3,130✔
596
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
3,130✔
597
        pStmt->exec.pCurrBlock = NULL;
3,130✔
598
      }
599
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
3,130✔
600
        resetRequest(pStmt);
3,130✔
601
      }
602
    } else {
603
      pStmt->sql.siInfo.pTableColsIdx = 0;
516,590✔
604
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
516,547✔
605
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
516,427✔
606
    }
607
    if (NULL != pStmt->exec.pRequest) {
519,867✔
608
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
516,737✔
609
    }
610
  } else {
611
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
14✔
612
      resetRequest(pStmt);
7✔
613
    }
614

615
    size_t keyLen = 0;
66✔
616
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
66✔
617
    while (pIter) {
285✔
618
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
219✔
619
      char*          key = taosHashGetKey(pIter, &keyLen);
219✔
620
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
219✔
621

622
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
219✔
623
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
25✔
624
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
58✔
625

626
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
25✔
627
        continue;
25✔
628
      }
629

630
      qDestroyStmtDataBlock(pBlocks);
194✔
631
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
194✔
632

633
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
194✔
634
    }
635

636
    if (keepTable) {
66✔
637
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
33✔
638
                 keepTable, deepClean);
639
      return TSDB_CODE_SUCCESS;
33✔
640
    }
641

642
    taosHashCleanup(pStmt->exec.pBlockHash);
33✔
643
    pStmt->exec.pBlockHash = NULL;
33✔
644

645
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
33✔
646
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
33✔
647
  }
648

649
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
519,900✔
650
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
519,740✔
651
             keepTable, deepClean);
652

653
  return TSDB_CODE_SUCCESS;
519,710✔
654
}
655

656
static void stmtFreeTbBuf(void* buf) {
3,130✔
657
  void* pBuf = *(void**)buf;
3,130✔
658
  taosMemoryFree(pBuf);
3,130✔
659
}
3,130✔
660

661
static void stmtFreeTbCols(void* buf) {
3,130,000✔
662
  SArray* pCols = *(SArray**)buf;
3,130,000✔
663
  taosArrayDestroy(pCols);
3,130,000✔
664
}
3,130,000✔
665

666
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
3,059✔
667
  STMT2_TLOG_E("start to free SQL info");
3,059✔
668

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

681
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
3,059✔
682
  while (pIter) {
3,084✔
683
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
25✔
684

685
    qDestroyStmtDataBlock(pCache->pDataCtx);
25✔
686
    qDestroyBoundColInfo(pCache->boundTags);
25✔
687
    taosMemoryFreeClear(pCache->boundTags);
25✔
688

689
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
25✔
690
  }
691
  taosHashCleanup(pStmt->sql.pTableCache);
3,059✔
692
  pStmt->sql.pTableCache = NULL;
3,059✔
693

694
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
3,059✔
695
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,059✔
696

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

707
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
3,059✔
708
  pStmt->sql.siInfo.tableColsReady = true;
3,059✔
709

710
  STMT2_TLOG_E("end to free SQL info");
3,059✔
711

712
  return TSDB_CODE_SUCCESS;
3,059✔
713
}
714

715
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
629,020✔
716
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
629,020✔
UNCOV
717
    return TSDB_CODE_SUCCESS;
×
718
  }
719

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

726
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
629,241✔
727
  if (TSDB_CODE_SUCCESS != code) {
629,215✔
UNCOV
728
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
729
    return code;
×
730
  }
731

732
  code =
733
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
629,215✔
734
  if (TSDB_CODE_SUCCESS != code) {
629,306✔
735
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
65✔
UNCOV
736
    return code;
×
737
  }
738

739
  *vgId = vgInfo.vgId;
629,241✔
740

741
  return TSDB_CODE_SUCCESS;
629,241✔
742
}
743

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

752
  pStmt->stat.ctgGetTbMetaNum++;
2,953✔
753

754
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
2,953✔
UNCOV
755
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
×
756
    (void)stmtCleanBindInfo(pStmt);
×
757

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

UNCOV
763
    STMT_ERR_RET(code);
×
764
  }
765

766
  STMT_ERR_RET(code);
2,953✔
767

768
  *uid = pTableMeta->uid;
2,953✔
769
  *suid = pTableMeta->suid;
2,953✔
770
  *tableType = pTableMeta->tableType;
2,953✔
771
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
2,953✔
772
  *vgId = pTableMeta->vgId;
2,953✔
773

774
  taosMemoryFree(pTableMeta);
2,953✔
775

776
  return TSDB_CODE_SUCCESS;
2,953✔
777
}
778

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

784
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
169✔
785

786
  return TSDB_CODE_SUCCESS;
169✔
787
}
788

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

796
  pStmt->bInfo.needParse = true;
3,324✔
797
  pStmt->bInfo.inExecCache = false;
3,324✔
798

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

UNCOV
804
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
805

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

812
  if (NULL == pStmt->pCatalog) {
3,280✔
813
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
3,051✔
814
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
3,007✔
815
  }
816

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

824
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
3,155✔
825

826
    return TSDB_CODE_SUCCESS;
3,155✔
827
  }
828

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

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

838
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
169✔
839
                      POINTER_BYTES)) {
UNCOV
840
        STMT_ERR_RET(terrno);
×
841
      }
842

843
      pStmt->exec.pCurrBlock = pNewBlock;
169✔
844

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

847
      return TSDB_CODE_SUCCESS;
169✔
848
    }
849

UNCOV
850
    STMT_RET(stmtCleanBindInfo(pStmt));
×
851
  }
852

UNCOV
853
  uint64_t uid, suid;
×
854
  int32_t  vgId;
×
855
  int8_t   tableType;
×
856

UNCOV
857
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
×
858

UNCOV
859
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
860

UNCOV
861
  if (uid == pStmt->bInfo.tbUid) {
×
862
    pStmt->bInfo.needParse = false;
×
863

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

UNCOV
866
    return TSDB_CODE_SUCCESS;
×
867
  }
868

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

UNCOV
875
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
876
    }
877

UNCOV
878
    pStmt->bInfo.needParse = false;
×
879

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

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

UNCOV
888
    return TSDB_CODE_SUCCESS;
×
889
  }
890

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

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

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

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

UNCOV
909
    pStmt->exec.pCurrBlock = pNewBlock;
×
910

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

UNCOV
913
    return TSDB_CODE_SUCCESS;
×
914
  }
915

UNCOV
916
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
917

UNCOV
918
  return TSDB_CODE_SUCCESS;
×
919
}
920

UNCOV
921
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
922
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
923

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

UNCOV
930
  pStmt->sql.status = STMT_INIT;
×
931

UNCOV
932
  return TSDB_CODE_SUCCESS;
×
933
}
934

935
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
1,586,401✔
936
  SStmtQNode* pParam = (SStmtQNode*)param;
1,586,401✔
937

938
  if (pParam->restoreTbCols) {
1,586,401✔
939
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,586,444✔
940
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
1,069,729✔
941
      *p = taosArrayInit(20, POINTER_BYTES);
1,069,729✔
942
      if (*p == NULL) {
1,069,595✔
UNCOV
943
        pStmt->errCode = terrno;
×
944
      }
945
    }
946
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
516,715✔
947
    STMT2_TLOG_E("restore pTableCols finished");
516,767✔
948
  } else {
949
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
1,069,595✔
950
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
951
    // taosMemoryFree(pParam->pTbData);
952
    if (code != TSDB_CODE_SUCCESS) {
1,069,640✔
UNCOV
953
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
954
      pStmt->errCode = code;
×
955
    }
956
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,069,640✔
957
  }
958
}
1,586,544✔
959

960
static void* stmtBindThreadFunc(void* param) {
3,130✔
961
  setThreadName("stmt2Bind");
3,130✔
962

963
  STscStmt2* pStmt = (STscStmt2*)param;
3,130✔
964
  STMT2_DLOG_E("stmt2 bind thread started");
3,130✔
965

966
  while (true) {
1,586,557✔
967
    SStmtQNode* asyncParam = NULL;
1,589,687✔
968

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

977
    stmtAsyncOutput(pStmt, asyncParam);
1,586,388✔
978
  }
979

980
  STMT2_DLOG_E("stmt2 bind thread stopped");
3,087✔
981
  return NULL;
3,130✔
982
}
983

984
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
3,130✔
985
  TdThreadAttr thAttr;
2,888✔
986
  if (taosThreadAttrInit(&thAttr) != 0) {
3,130✔
UNCOV
987
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
988
  }
989
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
3,130✔
UNCOV
990
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
991
  }
992

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

998
  pStmt->bindThreadInUse = true;
3,130✔
999

1000
  (void)taosThreadAttrDestroy(&thAttr);
3,130✔
1001
  return TSDB_CODE_SUCCESS;
3,130✔
1002
}
1003

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

1010
  return TSDB_CODE_SUCCESS;
3,130✔
1011
}
1012

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

1018
  return TSDB_CODE_SUCCESS;
3,059✔
1019
}
1020

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

1033
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
6,260✔
UNCOV
1034
    return terrno;
×
1035
  }
1036

1037
  pTblBuf->pCurBuff = buff;
3,130✔
1038
  pTblBuf->buffIdx = 1;
3,130✔
1039
  pTblBuf->buffOffset = 0;
3,130✔
1040

1041
  return TSDB_CODE_SUCCESS;
3,130✔
1042
}
1043

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

1049
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
3,059✔
1050
  if (NULL == pStmt) {
3,059✔
UNCOV
1051
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
1052
    return NULL;
×
1053
  }
1054

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

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

1070
  if (NULL != pOptions) {
3,059✔
1071
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
3,036✔
1072
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
3,036✔
1073
      pStmt->stbInterlaceMode = true;
3,026✔
1074
    }
1075

1076
    pStmt->reqid = pOptions->reqid;
3,036✔
1077
  }
1078

1079
  if (pStmt->stbInterlaceMode) {
3,059✔
1080
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
3,026✔
1081
    pStmt->sql.siInfo.acctId = taos->acctId;
3,026✔
1082
    pStmt->sql.siInfo.dbname = taos->db;
3,026✔
1083
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
3,026✔
1084

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

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

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

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

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

UNCOV
1135
    (void)stmtClose2(pStmt);
×
1136
    return NULL;
×
1137
  }
1138

1139
  pStmt->execSemWaited = false;
3,059✔
1140

1141
  // STMT_LOG_SEQ(STMT_INIT);
1142

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

1146
  return pStmt;
3,059✔
1147
}
1148

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

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

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

1180
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
104✔
1181
  if (NULL == pStmt->sql.siInfo.pTableHash) {
104✔
UNCOV
1182
    return terrno;
×
1183
  }
1184

1185
  pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
104✔
1186
  if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
104✔
UNCOV
1187
    return terrno;
×
1188
  }
1189

1190
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
104✔
1191
  if (NULL == pStmt->sql.siInfo.pTableCols) {
104✔
UNCOV
1192
    return terrno;
×
1193
  }
1194

1195
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
104✔
1196

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

1208
  return TSDB_CODE_SUCCESS;
104✔
1209
}
1210

1211
static int32_t stmtDeepReset(STscStmt2* pStmt) {
104✔
1212
  char*             db = pStmt->db;
104✔
1213
  TAOS_STMT2_OPTION options = pStmt->options;
104✔
1214
  uint32_t          reqid = pStmt->reqid;
104✔
1215

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

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

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

1240
      (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
104✔
1241
      (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
104✔
1242
    }
1243
  }
1244

1245
  pStmt->sql.autoCreateTbl = false;
104✔
1246
  taosMemoryFree(pStmt->sql.pBindInfo);
104✔
1247
  pStmt->sql.pBindInfo = NULL;
104✔
1248

1249
  taosMemoryFree(pStmt->sql.queryRes.fields);
104✔
1250
  pStmt->sql.queryRes.fields = NULL;
104✔
1251

1252
  taosMemoryFree(pStmt->sql.queryRes.userFields);
104✔
1253
  pStmt->sql.queryRes.userFields = NULL;
104✔
1254

1255
  pStmt->sql.type = 0;
104✔
1256
  pStmt->sql.runTimes = 0;
104✔
1257
  taosMemoryFree(pStmt->sql.sqlStr);
104✔
1258
  pStmt->sql.sqlStr = NULL;
104✔
1259

1260
  qDestroyQuery(pStmt->sql.pQuery);
104✔
1261
  pStmt->sql.pQuery = NULL;
104✔
1262

1263
  taosArrayDestroy(pStmt->sql.nodeList);
104✔
1264
  pStmt->sql.nodeList = NULL;
104✔
1265

1266
  taosHashCleanup(pStmt->sql.pVgHash);
104✔
1267
  pStmt->sql.pVgHash = NULL;
104✔
1268

1269
  if (pStmt->sql.fixValueTags) {
104✔
1270
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
104✔
1271
    pStmt->sql.fixValueTbReq = NULL;
104✔
1272
  }
1273
  pStmt->sql.fixValueTags = false;
104✔
1274

1275
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
104✔
1276
  while (pIter) {
104✔
UNCOV
1277
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
1278

UNCOV
1279
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
1280
    qDestroyBoundColInfo(pCache->boundTags);
×
1281
    taosMemoryFreeClear(pCache->boundTags);
×
1282

UNCOV
1283
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
1284
  }
1285
  taosHashCleanup(pStmt->sql.pTableCache);
104✔
1286

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

1292
  resetRequest(pStmt);
104✔
1293

1294
  if (pStmt->sql.siInfo.pTableCols) {
104✔
1295
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
104✔
1296
    pStmt->sql.siInfo.pTableCols = NULL;
104✔
1297
  }
1298

1299
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
104✔
1300
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
104✔
1301
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
104✔
1302
  }
1303

1304
  if (pStmt->sql.siInfo.pTableHash) {
104✔
1305
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
104✔
1306
    pStmt->sql.siInfo.pTableHash = NULL;
104✔
1307
  }
1308

1309
  if (pStmt->sql.siInfo.pTableRowDataHash) {
104✔
1310
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
104✔
1311
    pStmt->sql.siInfo.pTableRowDataHash = NULL;
104✔
1312
  }
1313

1314
  if (pStmt->sql.siInfo.pVgroupHash) {
104✔
UNCOV
1315
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1316
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1317
  }
1318

1319
  if (pStmt->sql.siInfo.pVgroupList) {
104✔
UNCOV
1320
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
1321
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1322
  }
1323

1324
  if (pStmt->sql.siInfo.pDataCtx) {
104✔
1325
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
104✔
1326
    pStmt->sql.siInfo.pDataCtx = NULL;
104✔
1327
  }
1328

1329
  if (pStmt->sql.siInfo.pTSchema) {
104✔
1330
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
104✔
1331
    pStmt->sql.siInfo.pTSchema = NULL;
104✔
1332
  }
1333

1334
  if (pStmt->sql.siInfo.pRequest) {
104✔
1335
    taos_free_result(pStmt->sql.siInfo.pRequest);
104✔
1336
    pStmt->sql.siInfo.pRequest = NULL;
104✔
1337
  }
1338

1339
  if (pStmt->stbInterlaceMode) {
104✔
1340
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
104✔
1341
  }
1342

1343
  pStmt->db = db;
104✔
1344
  pStmt->options = options;
104✔
1345
  pStmt->reqid = reqid;
104✔
1346

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

1353
  pStmt->sql.status = STMT_INIT;
104✔
1354

1355
  return TSDB_CODE_SUCCESS;
104✔
1356
}
1357

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

1362
  STMT2_DLOG("start to prepare with sql:%s", sql);
3,117✔
1363

1364
  if (stmt == NULL || sql == NULL) {
3,117✔
UNCOV
1365
    STMT2_ELOG_E("stmt or sql is NULL");
×
1366
    return TSDB_CODE_INVALID_PARA;
×
1367
  }
1368

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

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

1380
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
3,163✔
1381

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

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

1407
      if (pStmt->sql.stbInterlaceMode) {
3,095✔
1408
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
3,070✔
1409
      }
1410
    }
1411

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

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

1428
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3,130✔
1429
  pStmt->sql.siInfo.pDataCtx = pDst;
3,124✔
1430

1431
  SArray* pTblCols = NULL;
3,130✔
1432
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
3,111,142✔
1433
    pTblCols = taosArrayInit(20, POINTER_BYTES);
3,108,012✔
1434
    if (NULL == pTblCols) {
3,108,235✔
UNCOV
1435
      return terrno;
×
1436
    }
1437

1438
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
6,217,269✔
UNCOV
1439
      return terrno;
×
1440
    }
1441
  }
1442

1443
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
3,130✔
1444

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

1448
  return TSDB_CODE_SUCCESS;
3,130✔
1449
}
1450

1451
bool stmt2IsInsert(TAOS_STMT2* stmt) {
1,072,500✔
1452
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,072,500✔
1453
  if (pStmt->sql.type) {
1,072,500✔
1454
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,066,524✔
1455
  }
1456

1457
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
6,151✔
1458
}
1459

1460
bool stmt2IsSelect(TAOS_STMT2* stmt) {
3,140✔
1461
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,140✔
1462

1463
  if (pStmt->sql.type) {
3,140✔
UNCOV
1464
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1465
  }
1466
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
3,140✔
1467
}
1468

1469
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
1,069,110✔
1470
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,069,110✔
1471

1472
  int64_t startUs = taosGetTimestampUs();
1,068,533✔
1473

1474
  STMT2_TLOG("start to set tbName:%s", tbName);
1,068,533✔
1475

1476
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,068,728✔
UNCOV
1477
    return pStmt->errCode;
×
1478
  }
1479

1480
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
1,069,376✔
1481

1482
  int32_t insert = 0;
1,069,376✔
1483
  if (!stmt2IsInsert(stmt)) {
1,069,376✔
UNCOV
1484
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
1485
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1486
  }
1487
  // process tbname
1488
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,069,485✔
1489

1490
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,069,454✔
1491
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1492
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,069,336✔
1493
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
1,068,880✔
1494

1495
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
1,068,952✔
1496
    STMT_ERR_RET(stmtGetFromCache(pStmt));
3,488✔
1497

1498
    if (pStmt->bInfo.needParse) {
3,324✔
1499
      STMT_ERR_RET(stmtParseSql(pStmt));
3,155✔
1500
      if (!pStmt->sql.autoCreateTbl) {
3,155✔
1501
        uint64_t uid, suid;
2,888✔
1502
        int32_t  vgId;
2,888✔
1503
        int8_t   tableType;
2,888✔
1504

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

1512
  } else {
1513
    pStmt->exec.pRequest->requestId++;
1,065,593✔
1514
    pStmt->bInfo.needParse = false;
1,065,492✔
1515
  }
1516

1517
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,068,469✔
1518
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
3,130✔
1519
  }
1520

1521
  int64_t startUs2 = taosGetTimestampUs();
1,069,438✔
1522
  pStmt->stat.setTbNameUs += startUs2 - startUs;
1,069,438✔
1523

1524
  return TSDB_CODE_SUCCESS;
1,069,137✔
1525
}
1526

1527
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
525,279✔
1528
  STscStmt2* pStmt = (STscStmt2*)stmt;
525,279✔
1529

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

1535
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
525,370✔
UNCOV
1536
    return pStmt->errCode;
×
1537
  }
1538

1539
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
525,370✔
1540

1541
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
525,357✔
UNCOV
1542
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1543
    pStmt->bInfo.needParse = false;
×
1544
  }
1545
  STMT_ERR_RET(stmtCreateRequest(pStmt));
525,357✔
1546

1547
  if (pStmt->bInfo.needParse) {
525,266✔
UNCOV
1548
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1549
  }
1550
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
525,266✔
UNCOV
1551
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1552
  }
1553

1554
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
525,227✔
1555

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

1571
  STMT2_TLOG_E("start to bind stmt tag values");
525,227✔
1572

1573
  void* boundTags = NULL;
525,136✔
1574
  if (pStmt->sql.stbInterlaceMode) {
525,136✔
1575
    boundTags = pStmt->sql.siInfo.boundTags;
524,942✔
1576
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
524,942✔
1577
    if (NULL == pCreateTbReq) {
525,007✔
UNCOV
1578
      return terrno;
×
1579
    }
1580
    int32_t vgId = -1;
525,007✔
1581
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
525,007✔
1582
    (*pCreateTbReq)->uid = vgId;
525,280✔
1583
  } else {
1584
    boundTags = pStmt->bInfo.boundTags;
194✔
1585
  }
1586

1587
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
525,474✔
1588
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1589
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1590

1591
  return TSDB_CODE_SUCCESS;
525,357✔
1592
}
1593

1594
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
103,948✔
1595
  STscStmt2* pStmt = (STscStmt2*)stmt;
103,948✔
1596

1597
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
103,948✔
1598

1599
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
103,974✔
UNCOV
1600
    return pStmt->errCode;
×
1601
  }
1602

1603
  if (!pStmt->sql.stbInterlaceMode) {
103,974✔
UNCOV
1604
    return TSDB_CODE_SUCCESS;
×
1605
  }
1606

1607
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
103,974✔
1608

1609
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
103,935✔
UNCOV
1610
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1611
    pStmt->bInfo.needParse = false;
×
1612
  }
1613
  STMT_ERR_RET(stmtCreateRequest(pStmt));
103,935✔
1614

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

1623
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
103,935✔
UNCOV
1624
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1625
  }
1626

1627
  STableDataCxt** pDataBlock = NULL;
103,961✔
1628
  if (pStmt->exec.pCurrBlock) {
103,961✔
1629
    pDataBlock = &pStmt->exec.pCurrBlock;
103,831✔
1630
  } else {
1631
    pDataBlock =
1632
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
130✔
1633
    if (NULL == pDataBlock) {
130✔
UNCOV
1634
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1635
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
13✔
1636
    }
1637
  }
1638

1639
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
103,974✔
UNCOV
1640
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1641
    return TSDB_CODE_SUCCESS;
×
1642
  }
1643

1644
  if (pStmt->sql.fixValueTags) {
103,974✔
1645
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
103,870✔
1646
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
103,870✔
1647
    if ((*pCreateTbReq)->name) {
103,831✔
1648
      taosMemoryFree((*pCreateTbReq)->name);
103,831✔
1649
    }
1650
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
103,792✔
1651
    int32_t vgId = -1;
103,831✔
1652
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
103,831✔
1653
    (*pCreateTbReq)->uid = vgId;
103,831✔
1654
    return TSDB_CODE_SUCCESS;
103,831✔
1655
  }
1656

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

1665
  return TSDB_CODE_SUCCESS;
104✔
1666
}
1667

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

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

UNCOV
1678
  STableDataCxt** pDataBlock = NULL;
×
1679

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

UNCOV
1691
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1692

UNCOV
1693
  return TSDB_CODE_SUCCESS;
×
1694
}
1695

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

1700
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
23✔
UNCOV
1701
    return pStmt->errCode;
×
1702
  }
1703

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

1709
  STableDataCxt** pDataBlock = NULL;
23✔
1710
  bool            cleanStb = false;
23✔
1711

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

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

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

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

1746
  if (fieldNum != NULL) {
23✔
1747
    *fieldNum = totalNum;
23✔
1748
  }
1749

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

1753
_return:
23✔
1754

1755
  pStmt->errCode = preCode;
23✔
1756

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

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

1771
    *idx = pStmt->exec.smInfo.pColIdx;
1772
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1773
  }
1774
}
1775
*/
1776
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
1,069,344✔
1777
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
1,069,344✔
1778
    pStmt->sql.siInfo.pVgroupHash =
516,711✔
1779
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
516,534✔
1780
  }
1781
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
1,069,699✔
1782
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
516,702✔
1783
  }
1784

1785
  if (NULL == pStmt->sql.siInfo.pRequest) {
1,069,652✔
1786
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
3,130✔
1787
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1788

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

1794
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
3,130✔
1795
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
3,130✔
1796
  }
1797

1798
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
1,069,652✔
1799
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
20,368✔
1800
    pStmt->sql.siInfo.tbFromHash = true;
1,909✔
1801
  }
1802

1803
  if (0 == pStmt->sql.siInfo.firstName[0]) {
1,069,781✔
1804
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
3,026✔
1805
  }
1806

1807
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
1,069,781✔
1808
  param->next = NULL;
1,069,652✔
1809

1810
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,069,735✔
1811

1812
  if (pStmt->queue.stopQueue) {
1,069,735✔
UNCOV
1813
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1814
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1815
  }
1816
  stmtEnqueue(pStmt, param);
1,069,606✔
1817

1818
  return TSDB_CODE_SUCCESS;
1,069,542✔
1819
}
1820

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

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

1841
  return TSDB_CODE_SUCCESS;
1,069,234✔
1842
}
1843

1844
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
194✔
1845
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
194✔
UNCOV
1846
    return TSDB_CODE_SUCCESS;
×
1847
  }
1848

1849
  uint64_t uid = pStmt->bInfo.tbUid;
194✔
1850
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
194✔
1851

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

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

1864
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
25✔
1865

1866
  SStmtTableCache cache = {
25✔
1867
      .pDataCtx = pDst,
1868
      .boundTags = pStmt->bInfo.boundTags,
25✔
1869
  };
1870

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

1876
  if (pStmt->sql.autoCreateTbl) {
25✔
1877
    pStmt->bInfo.tagsCached = true;
25✔
1878
  } else {
UNCOV
1879
    pStmt->bInfo.boundTags = NULL;
×
1880
  }
1881

1882
  return TSDB_CODE_SUCCESS;
25✔
1883
}
1884

1885
static int stmtAddBatch2(TAOS_STMT2* stmt) {
516,875✔
1886
  STscStmt2* pStmt = (STscStmt2*)stmt;
516,875✔
1887

1888
  int64_t startUs = taosGetTimestampUs();
516,918✔
1889

1890
  // STMT2_TLOG_E("start to add batch");
1891

1892
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
516,918✔
UNCOV
1893
    return pStmt->errCode;
×
1894
  }
1895

1896
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
516,789✔
1897

1898
  if (pStmt->sql.stbInterlaceMode) {
516,771✔
1899
    int64_t startUs2 = taosGetTimestampUs();
516,638✔
1900
    pStmt->stat.addBatchUs += startUs2 - startUs;
516,638✔
1901

1902
    pStmt->sql.siInfo.tableColsReady = false;
516,724✔
1903

1904
    SStmtQNode* param = NULL;
516,638✔
1905
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,033,263✔
1906
    param->restoreTbCols = true;
516,625✔
1907
    param->next = NULL;
516,711✔
1908

1909
    if (pStmt->sql.autoCreateTbl) {
516,582✔
1910
      pStmt->bInfo.tagsCached = true;
209,016✔
1911
    }
1912
    pStmt->bInfo.boundColsCached = true;
516,453✔
1913

1914
    if (pStmt->queue.stopQueue) {
516,711✔
UNCOV
1915
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1916
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1917
    }
1918

1919
    stmtEnqueue(pStmt, param);
516,450✔
1920

1921
    return TSDB_CODE_SUCCESS;
516,724✔
1922
  }
1923

1924
  STMT_ERR_RET(stmtCacheBlock(pStmt));
194✔
1925

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

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

1943
  return TSDB_CODE_SUCCESS;
1944
}
1945

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

1950
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1951
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1952

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

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

1969
  return TSDB_CODE_SUCCESS;
1970
}
1971
*/
1972

1973
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
1,069,351✔
1974
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,069,351✔
1975
  int32_t    code = 0;
1,069,351✔
1976

1977
  int64_t startUs = taosGetTimestampUs();
1,069,563✔
1978

1979
  if (qDebugFlag & DEBUG_TRACE) {
1,069,563✔
UNCOV
1980
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1981
  }
1982

1983
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,069,472✔
UNCOV
1984
    return pStmt->errCode;
×
1985
  }
1986

1987
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
1,069,260✔
1988

1989
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,069,249✔
UNCOV
1990
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1991
    pStmt->bInfo.needParse = false;
×
1992
  }
1993

1994
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,069,231✔
UNCOV
1995
    resetRequest(pStmt);
×
1996
  }
1997

1998
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,069,056✔
1999
  if (pStmt->bInfo.needParse) {
1,069,058✔
UNCOV
2000
    code = stmtParseSql(pStmt);
×
2001
    if (code != TSDB_CODE_SUCCESS) {
×
2002
      goto cleanup_root;
×
2003
    }
2004
  }
2005

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

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

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

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

2049
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2050

2051
    return TSDB_CODE_SUCCESS;
8✔
2052

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

2062
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,069,397✔
UNCOV
2063
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2064
  }
2065

2066
  STableDataCxt** pDataBlock = NULL;
1,069,207✔
2067

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

2089
  int64_t startUs2 = taosGetTimestampUs();
1,069,421✔
2090
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
1,069,421✔
2091

2092
  SStmtQNode* param = NULL;
1,069,209✔
2093
  if (pStmt->sql.stbInterlaceMode) {
1,069,378✔
2094
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
2,138,479✔
2095
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
2,138,420✔
2096
    taosArrayClear(param->tblData.aCol);
1,069,234✔
2097

2098
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2099

2100
    param->restoreTbCols = false;
1,067,798✔
2101
    param->tblData.isOrdered = true;
1,067,841✔
2102
    param->tblData.isDuplicateTs = false;
1,067,927✔
2103
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
1,068,274✔
2104

2105
    param->pCreateTbReq = pCreateTbReq;
1,067,979✔
2106
  }
2107

2108
  int64_t startUs3 = taosGetTimestampUs();
1,068,953✔
2109
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
1,068,953✔
2110

2111
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
1,068,827✔
2112
  SBlobSet* pBlob = NULL;
1,068,824✔
2113
  if (colIdx < 0) {
1,068,907✔
2114
    if (pStmt->sql.stbInterlaceMode) {
1,069,030✔
2115
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
1,069,137✔
2116
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
1,394,699✔
2117
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
1,069,401✔
2118
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
1,069,573✔
2119
      param->tblData.isOrdered = (*pDataBlock)->ordered;
1,069,393✔
2120
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
1,069,221✔
2121
    } else {
2122
      if (colIdx == -1) {
129✔
2123
        if (pStmt->sql.bindRowFormat) {
194✔
UNCOV
2124
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2125
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2126
        }
2127
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
194✔
2128
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
194✔
2129
      } else {
2130
        code =
UNCOV
2131
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
×
UNCOV
2132
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
2133
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
129✔
2134
      }
2135
    }
2136

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

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

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

UNCOV
2157
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2158

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

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

2172
  int64_t startUs4 = taosGetTimestampUs();
1,069,841✔
2173
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
1,069,841✔
2174

2175
  if (pStmt->stbInterlaceMode) {
1,069,626✔
2176
    if (param) param->tblData.pBlobSet = pBlob;
1,069,613✔
2177
  }
2178

2179
  if (pStmt->sql.stbInterlaceMode) {
1,069,583✔
2180
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
1,069,494✔
2181
  } else {
2182
    STMT_ERR_RET(stmtAddBatch2(pStmt));
147✔
2183
  }
2184

2185
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
1,069,795✔
2186
  return TSDB_CODE_SUCCESS;
1,069,838✔
2187
}
2188

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

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

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

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

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

2219
      break;
2220
    }
2221

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

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

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

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

2253
      pStmt->stat.ctgGetTbMetaNum++;
2254

2255
      taos_free_result(pStmt->exec.pRequest);
2256
      pStmt->exec.pRequest = NULL;
2257

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

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

2270
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2271
  }
2272

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

2285
  STMT_DLOG_E("start to exec");
2286

2287
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2288

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

2292
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2293

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

2305
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2306

2307
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2308
  pStmt->affectedRows += pStmt->exec.affectedRows;
2309

2310
_return:
2311

2312
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2313

2314
  tFreeSSubmitRsp(pRsp);
2315

2316
  ++pStmt->sql.runTimes;
2317

2318
  STMT_RET(code);
2319
}
2320
*/
2321

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

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

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

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

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

UNCOV
2368
  if (fp) {
×
2369
    fp(pStmt->options.userdata, res, code);
×
2370
  }
2371

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

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

2386
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
516,619✔
2387
  STscStmt2* pStmt = (STscStmt2*)stmt;
516,619✔
2388
  int32_t    code = 0;
516,619✔
2389
  int64_t    startUs = taosGetTimestampUs();
516,727✔
2390

2391
  STMT2_DLOG_E("start to exec");
516,727✔
2392

2393
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
516,701✔
UNCOV
2394
    return pStmt->errCode;
×
2395
  }
2396

2397
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
516,744✔
2398
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
516,826✔
UNCOV
2399
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2400
  }
2401
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
516,675✔
2402

2403
  if (pStmt->sql.stbInterlaceMode) {
516,826✔
2404
    STMT_ERR_RET(stmtAddBatch2(pStmt));
516,707✔
2405
  }
2406

2407
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
516,727✔
2408

2409
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
516,761✔
2410
    if (pStmt->sql.stbInterlaceMode) {
516,723✔
2411
      int64_t startTs = taosGetTimestampUs();
516,793✔
2412
      // wait for stmt bind thread to finish
2413
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
1,933,512✔
2414
        taosUsleep(1);
1,416,680✔
2415
      }
2416

2417
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
516,780✔
UNCOV
2418
        return pStmt->errCode;
×
2419
      }
2420

2421
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
516,767✔
2422
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
516,767✔
2423
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
516,677✔
2424
      pStmt->sql.siInfo.pVgroupHash = NULL;
516,662✔
2425
      pStmt->sql.siInfo.pVgroupList = NULL;
516,668✔
2426
    } else {
2427
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
104✔
2428
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
25✔
2429

2430
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
25✔
2431

2432
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
25✔
2433
    }
2434
  }
2435

2436
  pStmt->asyncResultAvailable = false;
516,768✔
2437
  SRequestObj*      pRequest = pStmt->exec.pRequest;
516,639✔
2438
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
516,645✔
2439
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
516,774✔
2440
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2441

2442
  if (!fp) {
516,673✔
2443
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
516,673✔
2444

2445
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
516,563✔
2446
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
13✔
2447
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
13✔
2448
      if (code) {
×
2449
        pStmt->exec.pRequest->code = code;
×
2450

2451
      } else {
UNCOV
2452
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2453
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2454
      }
2455
    }
2456

2457
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
516,633✔
2458

2459
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
516,593✔
2460
    if (affected_rows) {
516,593✔
2461
      *affected_rows = pStmt->exec.affectedRows;
516,480✔
2462
    }
2463
    pStmt->affectedRows += pStmt->exec.affectedRows;
516,418✔
2464

2465
    // wait for stmt bind thread to finish
2466
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
516,688✔
UNCOV
2467
      taosUsleep(1);
×
2468
    }
2469

2470
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
516,522✔
2471

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

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

2492
_return:
516,616✔
2493
  if (code) {
516,616✔
UNCOV
2494
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2495
  }
2496
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
516,521✔
2497

2498
  STMT_RET(code);
516,601✔
2499
}
2500

2501
int stmtClose2(TAOS_STMT2* stmt) {
3,059✔
2502
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,059✔
2503

2504
  STMT2_DLOG_E("start to close stmt");
3,059✔
2505
  taosMemoryFreeClear(pStmt->db);
3,059✔
2506

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

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

2518
    (void)taosThreadJoin(pStmt->bindThread, NULL);
3,026✔
2519
    pStmt->bindThreadInUse = false;
3,026✔
2520

2521
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
3,026✔
2522
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
3,026✔
2523
  }
2524

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

2531
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
3,059✔
2532
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
3,059✔
2533

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

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

2555
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
3,059✔
2556

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

2564
  return TSDB_CODE_SUCCESS;
3,059✔
2565
}
2566

UNCOV
2567
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
2568
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2569

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

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

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

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

2591
int stmtParseColFields2(TAOS_STMT2* stmt) {
23✔
2592
  int32_t    code = 0;
23✔
2593
  STscStmt2* pStmt = (STscStmt2*)stmt;
23✔
2594
  int32_t    preCode = pStmt->errCode;
23✔
2595

2596
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
23✔
UNCOV
2597
    return pStmt->errCode;
×
2598
  }
2599

2600
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
23✔
UNCOV
2601
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2602
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2603
  }
2604

2605
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
23✔
2606

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

2615
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
23✔
2616

2617
  if (pStmt->bInfo.needParse) {
23✔
2618
    STMT_ERRI_JRET(stmtParseSql(pStmt));
23✔
2619
  }
2620

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

2627
  pStmt->errCode = preCode;
23✔
2628

2629
  return code;
23✔
2630
}
2631

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

2638
  return stmtFetchStbColFields2(stmt, nums, fields);
23✔
2639
}
2640

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

UNCOV
2646
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2647
    return pStmt->errCode;
×
2648
  }
2649

UNCOV
2650
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2651

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

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

UNCOV
2661
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2662

UNCOV
2663
  if (pStmt->bInfo.needParse) {
×
2664
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2665
  }
2666

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

UNCOV
2673
  STMT2_DLOG("get param num success, nums:%d", *nums);
×
2674

UNCOV
2675
_return:
×
2676

UNCOV
2677
  pStmt->errCode = preCode;
×
2678

UNCOV
2679
  return code;
×
2680
}
2681

2682
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
8✔
2683
  STscStmt2* pStmt = (STscStmt2*)stmt;
8✔
2684

2685
  STMT2_TLOG_E("start to use result");
8✔
2686

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

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

2697
  if (tsUseAdapter) {
8✔
UNCOV
2698
    TAOS_RES* res = (TAOS_RES*)pStmt->exec.pRequest;
×
2699
    pStmt->exec.pRequest = NULL;
×
2700
    return res;
×
2701
  }
2702

2703
  return pStmt->exec.pRequest;
8✔
2704
}
2705

UNCOV
2706
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2707
  qInfo("async stmt bind thread started");
×
2708

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

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

UNCOV
2720
  qInfo("async stmt bind thread stopped");
×
2721

UNCOV
2722
  return code;
×
2723
}
2724

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

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

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

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

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

UNCOV
2750
  return;
×
2751
}
2752

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

UNCOV
2757
  return errorCode;
×
2758
}
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