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

taosdata / TDengine / #4295

14 Jun 2025 08:14AM UTC coverage: 62.777% (-0.1%) from 62.881%
#4295

push

travis-ci

web-flow
refactor: update container_build.sh to include branch option for taosadapter (#31367)

157612 of 320085 branches covered (49.24%)

Branch coverage included in aggregate %.

243488 of 318844 relevant lines covered (76.37%)

6438123.12 hits per line

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

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

5
#include "clientStmt.h"
6
#include "clientStmt2.h"
7
/*
8
char* gStmtStatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
9
                          "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
10
*/
11
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
12
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
9,511✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
9,516✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
9,516✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
16
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
17
    if (NULL == pTblBuf->pCurBuff) {
×
18
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
19
    }
20
    *pBuf = pTblBuf->pCurBuff;
×
21
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
22
  } else {
23
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
24
    if (NULL == buff) {
×
25
      return terrno;
×
26
    }
27

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

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

38
  return TSDB_CODE_SUCCESS;
9,516✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
9,529✔
42
  int i = 0;
9,529✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
53,146✔
44
    if (pStmt->queue.stopQueue) {
43,664✔
45
      return false;
91✔
46
    }
47
    if (i < 10) {
43,573✔
48
      taosUsleep(1);
41,368✔
49
      i++;
41,334✔
50
    } else {
51
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,205✔
52
      if (pStmt->queue.stopQueue) {
2,284!
53
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
54
        return false;
×
55
      }
56
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
2,284✔
57
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
2,276✔
58
      }
59
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,284✔
60
    }
61
  }
62

63
  if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
9,301!
64
    return false;
×
65
  }
66

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

75
  SStmtQNode* node = pStmt->queue.head->next;
9,435✔
76
  pStmt->queue.head->next = node->next;
9,435✔
77
  if (pStmt->queue.tail == node) {
9,435✔
78
    pStmt->queue.tail = pStmt->queue.head;
5,217✔
79
  }
80
  node->next = NULL;
9,435✔
81
  *param = node;
9,435✔
82

83
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
9,435✔
84
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
9,437✔
85

86
  STMT2_DLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
9,438!
87

88
  return true;
9,439✔
89
}
90

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

97
  param->next = NULL;
9,424✔
98

99
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
9,424✔
100

101
  pStmt->queue.tail->next = param;
9,433✔
102
  pStmt->queue.tail = param;
9,433✔
103
  pStmt->stat.bindDataNum++;
9,433✔
104

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

108
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
9,436✔
109

110
  STMT2_TLOG("enqueue param:%p, remainNum:%" PRId64 ", restoreTbCols:%d", param, pStmt->queue.qRemainNum,
9,435!
111
             param->restoreTbCols);
112
}
113

114
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
6,011✔
115
  int32_t code = 0;
6,011✔
116

117
  if (pStmt->exec.pRequest == NULL) {
6,011✔
118
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
185✔
119
                        pStmt->reqid);
120
    if (pStmt->reqid != 0) {
184!
121
      pStmt->reqid++;
×
122
    }
123
    pStmt->exec.pRequest->type = RES_TYPE__QUERY;
184✔
124
    if (pStmt->db != NULL) {
184✔
125
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
138!
126
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
138!
127
    }
128
    if (TSDB_CODE_SUCCESS == code) {
184!
129
      pStmt->exec.pRequest->syncQuery = true;
184✔
130
      pStmt->exec.pRequest->isStmtBind = true;
184✔
131
    }
132
  }
133

134
  STMT2_TLOG("stmtCreateRequest, pRequest:%p, code:%d, db:%s", pStmt->exec.pRequest, code, pStmt->db);
6,010!
135

136
  return code;
6,009✔
137
}
138

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

142
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
19,626!
143
    STMT_LOG_SEQ(newStatus);
19,635!
144
  }
145

146
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
19,646!
147
    STMT2_ELOG("stmt already failed with err:%s, please use stmt prepare", tstrerror(pStmt->errCode));
×
148
    return pStmt->errCode;
×
149
  }
150

151
  switch (newStatus) {
19,646!
152
    case STMT_PREPARE:
158✔
153
      pStmt->errCode = 0;
158✔
154
      break;
158✔
155
    case STMT_SETTBNAME:
5,343✔
156
      if (STMT_STATUS_EQ(INIT)) {
5,343!
157
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
158
      }
159
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
5,343!
160
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
      }
162
      break;
5,343✔
163
    case STMT_SETTAGS:
164✔
164
      if (STMT_STATUS_EQ(INIT)) {
164!
165
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
166
      }
167
      break;
164✔
168
    case STMT_FETCH_FIELDS:
70✔
169
      if (STMT_STATUS_EQ(INIT)) {
70!
170
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
171
      }
172
      break;
70✔
173
    case STMT_BIND:
5,356✔
174
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
5,356!
175
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
176
      }
177
      /*
178
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
179
              code = TSDB_CODE_TSC_STMT_API_ERROR;
180
            }
181
      */
182
      break;
5,356✔
183
    case STMT_BIND_COL:
×
184
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
185
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
186
      }
187
      break;
×
188
    case STMT_ADD_BATCH:
4,307✔
189
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
4,307!
190
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
191
      }
192
      break;
4,307✔
193
    case STMT_EXECUTE:
4,248✔
194
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
4,248✔
195
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
5!
196
            STMT_STATUS_NE(BIND_COL)) {
×
197
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
198
        }
199
      } else {
200
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
4,243!
201
          code = TSDB_CODE_TSC_STMT_API_ERROR;
1✔
202
        }
203
      }
204
      break;
4,248✔
205
    default:
×
206
      code = TSDB_CODE_APP_ERROR;
×
207
      break;
×
208
  }
209

210
  STMT_ERR_RET(code);
19,646✔
211

212
  pStmt->sql.status = newStatus;
19,645✔
213

214
  return TSDB_CODE_SUCCESS;
19,645✔
215
}
216

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

220
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
130✔
221

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

227
  *tbName = pStmt->bInfo.tbName;
98✔
228

229
  return TSDB_CODE_SUCCESS;
98✔
230
}
231

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

241
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
137✔
242
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
137✔
243
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
137✔
244

245
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
137✔
246
  pStmt->bInfo.tbSuid = pTableMeta->suid;
137✔
247
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
137✔
248
  pStmt->bInfo.tbType = pTableMeta->tableType;
137✔
249

250
  if (!pStmt->bInfo.tagsCached) {
137✔
251
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
133✔
252
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
130!
253
  }
254

255
  pStmt->bInfo.boundTags = tags;
135✔
256
  pStmt->bInfo.tagsCached = false;
135✔
257
  pStmt->bInfo.tbNameFlag = tbNameFlag;
135✔
258
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
135✔
259

260
  return TSDB_CODE_SUCCESS;
135✔
261
}
262

263
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
134✔
264
  STscStmt2* pStmt = (STscStmt2*)stmt;
134✔
265

266
  pStmt->sql.pVgHash = pVgHash;
134✔
267
  pStmt->exec.pBlockHash = pBlockHash;
134✔
268

269
  return TSDB_CODE_SUCCESS;
134✔
270
}
271

272
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
136✔
273
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, uint8_t tbNameFlag) {
274
  STscStmt2* pStmt = (STscStmt2*)stmt;
136✔
275

276
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
136!
277
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
135!
278

279
  pStmt->sql.autoCreateTbl = autoCreateTbl;
134✔
280

281
  return TSDB_CODE_SUCCESS;
134✔
282
}
283

284
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
11✔
285
  STscStmt2* pStmt = (STscStmt2*)stmt;
11✔
286

287
  *pVgHash = pStmt->sql.pVgHash;
11✔
288
  pStmt->sql.pVgHash = NULL;
11✔
289

290
  *pBlockHash = pStmt->exec.pBlockHash;
11✔
291
  pStmt->exec.pBlockHash = NULL;
11✔
292

293
  return TSDB_CODE_SUCCESS;
11✔
294
}
295

296
static int32_t stmtParseSql(STscStmt2* pStmt) {
165✔
297
  pStmt->exec.pCurrBlock = NULL;
165✔
298

299
  STMT2_DLOG_E("start to stmtParseSql");
165!
300

301
  SStmtCallback stmtCb = {
165✔
302
      .pStmt = pStmt,
303
      .getTbNameFn = stmtGetTbName,
304
      .setInfoFn = stmtUpdateInfo,
305
      .getExecInfoFn = stmtGetExecInfo,
306
  };
307

308
  STMT_ERR_RET(stmtCreateRequest(pStmt));
165!
309

310
  pStmt->stat.parseSqlNum++;
166✔
311
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
166✔
312

313
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
144✔
314

315
  pStmt->bInfo.needParse = false;
144✔
316

317
  if (pStmt->sql.type == 0) {
144✔
318
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
12!
319
      pStmt->sql.type = STMT_TYPE_INSERT;
11✔
320
      pStmt->sql.stbInterlaceMode = false;
11✔
321
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
1!
322
      pStmt->sql.type = STMT_TYPE_QUERY;
×
323
      pStmt->sql.stbInterlaceMode = false;
×
324

325
      return TSDB_CODE_SUCCESS;
×
326
    } else {
327
      pStmt->bInfo.needParse = true;
1✔
328
      STMT2_ELOG_E("only support select or insert sql");
1!
329
      if (pStmt->exec.pRequest->msgBuf) {
1!
330
        tstrncpy(pStmt->exec.pRequest->msgBuf, "stmt only support select or insert", pStmt->exec.pRequest->msgBufLen);
1✔
331
      }
332
      return TSDB_CODE_TSC_STMT_API_ERROR;
1✔
333
    }
334
  } else if (pStmt->sql.type == STMT_TYPE_QUERY) {
132✔
335
    pStmt->sql.stbInterlaceMode = false;
6✔
336
    return TSDB_CODE_SUCCESS;
6✔
337
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
126!
338
    pStmt->sql.stbInterlaceMode = false;
×
339
  }
340

341
  STableDataCxt** pSrc =
342
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
137✔
343
  if (NULL == pSrc || NULL == *pSrc) {
138!
344
    STMT2_ELOG("fail to get exec.pBlockHash, maybe parse failed, tbFName:%s", pStmt->bInfo.tbFName);
×
345
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
346
  }
347

348
  STableDataCxt* pTableCtx = *pSrc;
138✔
349
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
138✔
350
    STMT2_TLOG("destroy pCreateTbReq for no-using insert, tbFName:%s", pStmt->bInfo.tbFName);
10!
351
    tdDestroySVCreateTbReq(pTableCtx->pData->pCreateTbReq);
10!
352
    taosMemoryFreeClear(pTableCtx->pData->pCreateTbReq);
10!
353
    pTableCtx->pData->pCreateTbReq = NULL;
10✔
354
  }
355
  // if (pStmt->sql.stbInterlaceMode) {
356
  //   int16_t lastIdx = -1;
357

358
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
359
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
360
  //       pStmt->sql.stbInterlaceMode = false;
361
  //       break;
362
  //     }
363

364
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
365
  //   }
366
  // }
367

368
  if (NULL == pStmt->sql.pBindInfo) {
138✔
369
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
127!
370
    if (NULL == pStmt->sql.pBindInfo) {
127!
371
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
372
      return terrno;
×
373
    }
374
  }
375

376
  return TSDB_CODE_SUCCESS;
138✔
377
}
378

379
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
4,510✔
380
  pStmt->bInfo.tbUid = 0;
4,510✔
381
  pStmt->bInfo.tbVgId = -1;
4,510✔
382
  pStmt->bInfo.tbType = 0;
4,510✔
383
  pStmt->bInfo.needParse = true;
4,510✔
384
  pStmt->bInfo.inExecCache = false;
4,510✔
385

386
  pStmt->bInfo.tbName[0] = 0;
4,510✔
387
  pStmt->bInfo.tbFName[0] = 0;
4,510✔
388
  if (!pStmt->bInfo.tagsCached) {
4,510✔
389
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
4,429✔
390
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
4,430!
391
  }
392
  if (!pStmt->sql.autoCreateTbl) {
4,511✔
393
    pStmt->bInfo.stbFName[0] = 0;
4,385✔
394
    pStmt->bInfo.tbSuid = 0;
4,385✔
395
  }
396

397
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
4,511!
398
             pStmt->sql.autoCreateTbl);
399

400
  return TSDB_CODE_SUCCESS;
4,512✔
401
}
402

403
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
404
  (void)qResetStmtColumns(pTb->aCol, true);
×
405
  taosArrayDestroy(pTb->aCol);
×
406
}
×
407

408
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
4,188✔
409
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
4,188✔
410
  if (NULL == pTblBuf->pCurBuff) {
4,189✔
411
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
1!
412
    return;
×
413
  }
414
  pTblBuf->buffIdx = 1;
4,188✔
415
  pTblBuf->buffOffset = sizeof(*pQueue->head);
4,188✔
416

417
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
4,188✔
418
  pQueue->qRemainNum = 0;
4,188✔
419
  pQueue->head->next = NULL;
4,188✔
420
}
421

422
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
4,402✔
423
  if (pStmt->sql.stbInterlaceMode) {
4,402✔
424
    if (deepClean) {
4,274✔
425
      taosHashCleanup(pStmt->exec.pBlockHash);
84✔
426
      pStmt->exec.pBlockHash = NULL;
84✔
427

428
      if (NULL != pStmt->exec.pCurrBlock) {
84✔
429
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
79!
430
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
79!
431
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
79✔
432
        pStmt->exec.pCurrBlock = NULL;
79✔
433
      }
434
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
84!
435
        taos_free_result(pStmt->exec.pRequest);
84✔
436
        pStmt->exec.pRequest = NULL;
84✔
437
      }
438
    } else {
439
      pStmt->sql.siInfo.pTableColsIdx = 0;
4,190✔
440
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
4,190✔
441
    }
442
    if (NULL != pStmt->exec.pRequest) {
4,273✔
443
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
4,189✔
444
    }
445
  } else {
446
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
128✔
447
      // if (!pStmt->options.asyncExecFn) {
448
      taos_free_result(pStmt->exec.pRequest);
123✔
449
      pStmt->exec.pRequest = NULL;
123✔
450
      //}
451
    }
452

453
    size_t keyLen = 0;
128✔
454
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
128✔
455
    while (pIter) {
270✔
456
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
142✔
457
      char*          key = taosHashGetKey(pIter, &keyLen);
142✔
458
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
142✔
459

460
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
142✔
461
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
47✔
462
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
99!
463

464
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
47✔
465
        continue;
47✔
466
      }
467

468
      qDestroyStmtDataBlock(pBlocks);
95✔
469
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
95!
470

471
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
95✔
472
    }
473

474
    if (keepTable) {
128✔
475
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
52!
476
                 keepTable, deepClean);
477
      return TSDB_CODE_SUCCESS;
52✔
478
    }
479

480
    taosHashCleanup(pStmt->exec.pBlockHash);
76✔
481
    pStmt->exec.pBlockHash = NULL;
76✔
482

483
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
76✔
484
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
76!
485
  }
486

487
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
4,349!
488
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
4,348!
489
             keepTable, deepClean);
490

491
  return TSDB_CODE_SUCCESS;
4,350✔
492
}
493

494
static void stmtFreeTbBuf(void* buf) {
91✔
495
  void* pBuf = *(void**)buf;
91✔
496
  taosMemoryFree(pBuf);
91!
497
}
91✔
498

499
static void stmtFreeTbCols(void* buf) {
80,000✔
500
  SArray* pCols = *(SArray**)buf;
80,000✔
501
  taosArrayDestroy(pCols);
80,000✔
502
}
80,000✔
503

504
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
134✔
505
  STMT2_TLOG_E("start to free SQL info");
134!
506

507
  taosMemoryFree(pStmt->sql.pBindInfo);
134!
508
  taosMemoryFree(pStmt->sql.queryRes.fields);
134!
509
  taosMemoryFree(pStmt->sql.queryRes.userFields);
134!
510
  taosMemoryFree(pStmt->sql.sqlStr);
134!
511
  qDestroyQuery(pStmt->sql.pQuery);
134✔
512
  taosArrayDestroy(pStmt->sql.nodeList);
134✔
513
  taosHashCleanup(pStmt->sql.pVgHash);
134✔
514
  pStmt->sql.pVgHash = NULL;
134✔
515
  if (pStmt->sql.fixValueTags) {
134✔
516
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
5!
517
  }
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
134✔
520
  while (pIter) {
149✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
15✔
522

523
    qDestroyStmtDataBlock(pCache->pDataCtx);
15✔
524
    qDestroyBoundColInfo(pCache->boundTags);
15✔
525
    taosMemoryFreeClear(pCache->boundTags);
15!
526

527
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
15✔
528
  }
529
  taosHashCleanup(pStmt->sql.pTableCache);
134✔
530
  pStmt->sql.pTableCache = NULL;
134✔
531

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
134!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
134!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
134✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
134✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
134✔
538
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
134✔
539
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
134!
540
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
134✔
541
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
134✔
542
  pStmt->sql.siInfo.pTableCols = NULL;
134✔
543

544
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
134✔
545
  pStmt->sql.siInfo.tableColsReady = true;
134✔
546

547
  STMT2_TLOG_E("end to free SQL info");
134!
548

549
  return TSDB_CODE_SUCCESS;
134✔
550
}
551

552
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
131✔
553
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
131✔
554
    return TSDB_CODE_SUCCESS;
15✔
555
  }
556

557
  SVgroupInfo      vgInfo = {0};
116✔
558
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
116✔
559
                           .requestId = pStmt->exec.pRequest->requestId,
116✔
560
                           .requestObjRefId = pStmt->exec.pRequest->self,
116✔
561
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
116✔
562

563
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
116✔
564
  if (TSDB_CODE_SUCCESS != code) {
116!
565
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
566
    return code;
×
567
  }
568

569
  code =
570
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
116✔
571
  if (TSDB_CODE_SUCCESS != code) {
116!
572
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
×
573
    return code;
×
574
  }
575

576
  *vgId = vgInfo.vgId;
116✔
577

578
  return TSDB_CODE_SUCCESS;
116✔
579
}
580

581
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
66✔
582
                                    uint64_t suid, int32_t vgId) {
583
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
66!
584
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
66!
585

586
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
66!
587

588
  return TSDB_CODE_SUCCESS;
66✔
589
}
590

591
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
187✔
592
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
187!
593
    pStmt->bInfo.needParse = false;
×
594
    pStmt->bInfo.inExecCache = false;
×
595
    return TSDB_CODE_SUCCESS;
×
596
  }
597

598
  pStmt->bInfo.needParse = true;
187✔
599
  pStmt->bInfo.inExecCache = false;
187✔
600

601
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
187✔
602
  if (pCxtInExec) {
188✔
603
    pStmt->bInfo.needParse = false;
24✔
604
    pStmt->bInfo.inExecCache = true;
24✔
605

606
    pStmt->exec.pCurrBlock = *pCxtInExec;
24✔
607

608
    if (pStmt->sql.autoCreateTbl) {
24✔
609
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
18!
610
      return TSDB_CODE_SUCCESS;
18✔
611
    }
612
  }
613

614
  if (NULL == pStmt->pCatalog) {
170✔
615
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
77✔
616
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
77✔
617
  }
618

619
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
170!
620
    if (pStmt->bInfo.inExecCache) {
98!
621
      pStmt->bInfo.needParse = false;
×
622
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
623
      return TSDB_CODE_SUCCESS;
×
624
    }
625

626
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
98!
627
    return TSDB_CODE_SUCCESS;
98✔
628
  }
629

630
  if (pStmt->sql.autoCreateTbl) {
72✔
631
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
48✔
632
    if (pCache) {
48!
633
      pStmt->bInfo.needParse = false;
48✔
634
      pStmt->bInfo.tbUid = 0;
48✔
635

636
      STableDataCxt* pNewBlock = NULL;
48✔
637
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
48!
638

639
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
48!
640
                      POINTER_BYTES)) {
641
        STMT_ERR_RET(terrno);
×
642
      }
643

644
      pStmt->exec.pCurrBlock = pNewBlock;
48✔
645

646
      STMT2_DLOG("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
48!
647

648
      return TSDB_CODE_SUCCESS;
48✔
649
    }
650

651
    STMT_RET(stmtCleanBindInfo(pStmt));
×
652
  }
653

654
  uint64_t uid, suid;
655
  int32_t  vgId;
656
  int8_t   tableType;
657

658
  STableMeta*      pTableMeta = NULL;
24✔
659
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
24✔
660
                           .requestId = pStmt->exec.pRequest->requestId,
24✔
661
                           .requestObjRefId = pStmt->exec.pRequest->self,
24✔
662
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
24✔
663
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
24✔
664

665
  pStmt->stat.ctgGetTbMetaNum++;
24✔
666

667
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
24!
668
    STMT2_DLOG("tb %s not exist", pStmt->bInfo.tbFName);
×
669
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
670

671
    STMT_ERR_RET(code);
×
672
  }
673

674
  STMT_ERR_RET(code);
24!
675

676
  uid = pTableMeta->uid;
24✔
677
  suid = pTableMeta->suid;
24✔
678
  tableType = pTableMeta->tableType;
24✔
679
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
24✔
680
  vgId = pTableMeta->vgId;
24✔
681

682
  taosMemoryFree(pTableMeta);
24!
683

684
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
24!
685

686
  if (uid == pStmt->bInfo.tbUid) {
24!
687
    pStmt->bInfo.needParse = false;
×
688

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

691
    return TSDB_CODE_SUCCESS;
×
692
  }
693

694
  if (pStmt->bInfo.inExecCache) {
24✔
695
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
6✔
696
    if (NULL == pCache) {
6!
697
      STMT2_ELOG("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
698
                 pStmt->bInfo.tbFName, uid, cacheUid);
699

700
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
701
    }
702

703
    pStmt->bInfo.needParse = false;
6✔
704

705
    pStmt->bInfo.tbUid = uid;
6✔
706
    pStmt->bInfo.tbSuid = suid;
6✔
707
    pStmt->bInfo.tbType = tableType;
6✔
708
    pStmt->bInfo.boundTags = pCache->boundTags;
6✔
709
    pStmt->bInfo.tagsCached = true;
6✔
710

711
    STMT2_DLOG("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
6!
712

713
    return TSDB_CODE_SUCCESS;
6✔
714
  }
715

716
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
18✔
717
  if (pCache) {
18!
718
    pStmt->bInfo.needParse = false;
18✔
719

720
    pStmt->bInfo.tbUid = uid;
18✔
721
    pStmt->bInfo.tbSuid = suid;
18✔
722
    pStmt->bInfo.tbType = tableType;
18✔
723
    pStmt->bInfo.boundTags = pCache->boundTags;
18✔
724
    pStmt->bInfo.tagsCached = true;
18✔
725

726
    STableDataCxt* pNewBlock = NULL;
18✔
727
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
18!
728

729
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
18!
730
                    POINTER_BYTES)) {
731
      STMT_ERR_RET(terrno);
×
732
    }
733

734
    pStmt->exec.pCurrBlock = pNewBlock;
18✔
735

736
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
18!
737

738
    return TSDB_CODE_SUCCESS;
18✔
739
  }
740

741
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
742

743
  return TSDB_CODE_SUCCESS;
×
744
}
745

746
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
747
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
748

749
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
750
  if (NULL == pStmt->sql.pTableCache) {
×
751
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
752
    STMT_ERR_RET(terrno);
×
753
  }
754

755
  pStmt->sql.status = STMT_INIT;
×
756

757
  return TSDB_CODE_SUCCESS;
×
758
}
759

760
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
9,438✔
761
  SStmtQNode* pParam = (SStmtQNode*)param;
9,438✔
762

763
  if (pParam->restoreTbCols) {
9,438✔
764
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
9,437✔
765
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
5,245✔
766
      *p = taosArrayInit(20, POINTER_BYTES);
5,245✔
767
      if (*p == NULL) {
5,246!
768
        STMT_ERR_RET(terrno);
×
769
      }
770
    }
771
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
4,192✔
772
    STMT2_TLOG_E("restore pTableCols finished");
4,196!
773
  } else {
774
    int code = qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
5,242✔
775
                                      &pStmt->sql.siInfo, pParam->pCreateTbReq);
776
    // taosMemoryFree(pParam->pTbData);
777
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,237✔
778
    if (code != TSDB_CODE_SUCCESS) {
5,245!
779
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
780
      STMT_ERR_RET(code);
×
781
    }
782
  }
783
  return TSDB_CODE_SUCCESS;
9,439✔
784
}
785

786
static void* stmtBindThreadFunc(void* param) {
91✔
787
  setThreadName("stmt2Bind");
91✔
788

789
  STscStmt2* pStmt = (STscStmt2*)param;
91✔
790
  STMT2_ILOG_E("stmt2 bind thread started");
91!
791

792
  while (true) {
9,439✔
793
    SStmtQNode* asyncParam = NULL;
9,530✔
794

795
    if (!stmtDequeue(pStmt, &asyncParam)) {
9,530✔
796
      if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
91!
797
        STMT2_DLOG_E("queue is empty and stopQueue is set, thread will exit");
91!
798
        break;
91✔
799
      }
800
      continue;
×
801
    }
802

803
    int ret = stmtAsyncOutput(pStmt, asyncParam);
9,438✔
804
    if (ret != 0) {
9,439!
805
      STMT2_ELOG("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
806
    }
807
  }
808

809
  STMT2_ILOG_E("stmt2 bind thread stopped");
91!
810
  return NULL;
91✔
811
}
812

813
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
91✔
814
  TdThreadAttr thAttr;
815
  if (taosThreadAttrInit(&thAttr) != 0) {
91!
816
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
817
  }
818
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
91!
819
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
820
  }
821

822
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
91!
823
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
824
    STMT_ERR_RET(terrno);
×
825
  }
826

827
  pStmt->bindThreadInUse = true;
91✔
828

829
  (void)taosThreadAttrDestroy(&thAttr);
91✔
830
  return TSDB_CODE_SUCCESS;
91✔
831
}
832

833
static int32_t stmtInitQueue(STscStmt2* pStmt) {
91✔
834
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
91✔
835
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
91✔
836
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
182!
837
  pStmt->queue.tail = pStmt->queue.head;
91✔
838

839
  return TSDB_CODE_SUCCESS;
91✔
840
}
841

842
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
136✔
843
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
136✔
844
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
136✔
845
  pStmt->asyncBindParam.asyncBindNum = 0;
136✔
846

847
  return TSDB_CODE_SUCCESS;
136✔
848
}
849

850
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
91✔
851
  pTblBuf->buffUnit = sizeof(SStmtQNode);
91✔
852
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
91✔
853
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
91✔
854
  if (NULL == pTblBuf->pBufList) {
91!
855
    return terrno;
×
856
  }
857
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
91!
858
  if (NULL == buff) {
91!
859
    return terrno;
×
860
  }
861

862
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
182!
863
    return terrno;
×
864
  }
865

866
  pTblBuf->pCurBuff = buff;
91✔
867
  pTblBuf->buffIdx = 1;
91✔
868
  pTblBuf->buffOffset = 0;
91✔
869

870
  return TSDB_CODE_SUCCESS;
91✔
871
}
872

873
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
135✔
874
  STscObj*   pObj = (STscObj*)taos;
135✔
875
  STscStmt2* pStmt = NULL;
135✔
876
  int32_t    code = 0;
135✔
877

878
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
135!
879
  if (NULL == pStmt) {
136!
880
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
881
    return NULL;
×
882
  }
883

884
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
136✔
885
  if (NULL == pStmt->sql.pTableCache) {
136!
886
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtInit2:%s", tstrerror(terrno));
×
887
    taosMemoryFree(pStmt);
×
888
    return NULL;
×
889
  }
890

891
  pStmt->taos = pObj;
136✔
892
  if (taos->db[0] != '\0') {
136✔
893
    pStmt->db = taosStrdup(taos->db);
54!
894
  }
895
  pStmt->bInfo.needParse = true;
136✔
896
  pStmt->sql.status = STMT_INIT;
136✔
897
  pStmt->errCode = TSDB_CODE_SUCCESS;
136✔
898

899
  if (NULL != pOptions) {
136!
900
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
136✔
901
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
136✔
902
      pStmt->stbInterlaceMode = true;
69✔
903
    }
904

905
    pStmt->reqid = pOptions->reqid;
136✔
906
  }
907

908
  if (pStmt->stbInterlaceMode) {
136✔
909
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
69✔
910
    pStmt->sql.siInfo.acctId = taos->acctId;
69✔
911
    pStmt->sql.siInfo.dbname = taos->db;
69✔
912
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
69✔
913
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
69✔
914
    if (NULL == pStmt->sql.siInfo.pTableHash) {
69!
915
      STMT2_ELOG("fail to allocate memory for pTableHash:%s", tstrerror(terrno));
×
916
      (void)stmtClose2(pStmt);
×
917
      return NULL;
×
918
    }
919
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
69✔
920
    if (NULL == pStmt->sql.siInfo.pTableCols) {
69!
921
      STMT2_ELOG("fail to allocate memory for pTableCols:%s", tstrerror(terrno));
×
922
      (void)stmtClose2(pStmt);
×
923
      return NULL;
×
924
    }
925

926
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
69✔
927
    if (TSDB_CODE_SUCCESS == code) {
69!
928
      code = stmtInitQueue(pStmt);
69✔
929
    }
930
    if (TSDB_CODE_SUCCESS == code) {
69!
931
      code = stmtStartBindThread(pStmt);
69✔
932
    }
933
    if (TSDB_CODE_SUCCESS != code) {
69!
934
      terrno = code;
×
935
      STMT2_ELOG("fail to init stmt2 bind thread:%s", tstrerror(code));
×
936
      (void)stmtClose2(pStmt);
×
937
      return NULL;
×
938
    }
939
  }
940

941
  pStmt->sql.siInfo.tableColsReady = true;
136✔
942
  if (pStmt->options.asyncExecFn) {
136✔
943
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
6!
944
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
945
      STMT2_ELOG("fail to init asyncExecSem:%s", tstrerror(terrno));
×
946
      (void)stmtClose2(pStmt);
×
947
      return NULL;
×
948
    }
949
  }
950
  code = stmtIniAsyncBind(pStmt);
136✔
951
  if (TSDB_CODE_SUCCESS != code) {
136!
952
    terrno = code;
×
953
    STMT2_ELOG("fail to start init asyncExecSem:%s", tstrerror(code));
×
954

955
    (void)stmtClose2(pStmt);
×
956
    return NULL;
×
957
  }
958

959
  pStmt->execSemWaited = false;
136✔
960

961
  // STMT_LOG_SEQ(STMT_INIT);
962

963
  STMT2_DLOG("stmt2 initialize finished, seqId:%d, db:%s, interlaceMode:%d, asyncExec:%d", pStmt->seqId, pStmt->db,
136!
964
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
965

966
  return pStmt;
136✔
967
}
968

969
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
69✔
970
  STscStmt2* pStmt = (STscStmt2*)stmt;
69✔
971
  if (dbName == NULL || dbName[0] == '\0') {
69!
972
    STMT2_ELOG_E("dbname in sql is illegal");
×
973
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
974
  }
975

976
  STMT2_DLOG("dbname is specified in sql:%s", dbName);
69!
977
  if (pStmt->db == NULL || pStmt->db[0] == '\0') {
69!
978
    taosMemoryFreeClear(pStmt->db);
35!
979
    STMT2_DLOG("dbname:%s is by sql, not by taosconnect", dbName);
35!
980
    pStmt->db = taosStrdup(dbName);
35!
981
    (void)strdequote(pStmt->db);
35✔
982
  }
983
  STMT_ERR_RET(stmtCreateRequest(pStmt));
69!
984

985
  // The SQL statement specifies a database name, overriding the previously specified database
986
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
69!
987
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
69!
988
  (void)strdequote(pStmt->exec.pRequest->pDb);
69✔
989
  if (pStmt->exec.pRequest->pDb == NULL) {
69!
990
    return terrno;
×
991
  }
992
  if (pStmt->sql.stbInterlaceMode) {
69✔
993
    pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
27✔
994
  }
995
  return TSDB_CODE_SUCCESS;
69✔
996
}
997
static int32_t stmtResetStbInterlaceCache(STscStmt2* pStmt) {
22✔
998
  int32_t code = TSDB_CODE_SUCCESS;
22✔
999

1000
  if (pStmt->bindThreadInUse) {
22!
1001
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
22!
1002
      taosUsleep(1);
×
1003
    }
1004
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
22✔
1005
    pStmt->queue.stopQueue = true;
22✔
1006
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
22✔
1007
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
22✔
1008

1009
    (void)taosThreadJoin(pStmt->bindThread, NULL);
22✔
1010
    pStmt->bindThreadInUse = false;
22✔
1011
    pStmt->queue.head = NULL;
22✔
1012
    pStmt->queue.tail = NULL;
22✔
1013
    pStmt->queue.qRemainNum = 0;
22✔
1014

1015
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
22✔
1016
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
22✔
1017
  }
1018

1019
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
22✔
1020
  if (NULL == pStmt->sql.siInfo.pTableHash) {
22!
1021
    return terrno;
×
1022
  }
1023

1024
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
22✔
1025
  if (NULL == pStmt->sql.siInfo.pTableCols) {
22!
1026
    return terrno;
×
1027
  }
1028

1029
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
22✔
1030

1031
  if (TSDB_CODE_SUCCESS == code) {
22!
1032
    code = stmtInitQueue(pStmt);
22✔
1033
    pStmt->queue.stopQueue = false;
22✔
1034
  }
1035
  if (TSDB_CODE_SUCCESS == code) {
22!
1036
    code = stmtStartBindThread(pStmt);
22✔
1037
  }
1038
  if (TSDB_CODE_SUCCESS != code) {
22!
1039
    return code;
×
1040
  }
1041

1042
  return TSDB_CODE_SUCCESS;
22✔
1043
}
1044

1045
static int32_t stmtDeepReset(STscStmt2* pStmt) {
26✔
1046
  char*             db = pStmt->db;
26✔
1047
  bool              stbInterlaceMode = pStmt->stbInterlaceMode;
26✔
1048
  TAOS_STMT2_OPTION options = pStmt->options;
26✔
1049
  uint32_t          reqid = pStmt->reqid;
26✔
1050

1051
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
26!
1052
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
2!
1053
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1054
    }
1055
    pStmt->execSemWaited = true;
2✔
1056
  }
1057
  taosMemoryFree(pStmt->sql.pBindInfo);
26!
1058
  pStmt->sql.pBindInfo = NULL;
26✔
1059

1060
  taosMemoryFree(pStmt->sql.queryRes.fields);
26!
1061
  pStmt->sql.queryRes.fields = NULL;
26✔
1062

1063
  taosMemoryFree(pStmt->sql.queryRes.userFields);
26!
1064
  pStmt->sql.queryRes.userFields = NULL;
26✔
1065

1066
  pStmt->sql.type = 0;
26✔
1067
  pStmt->sql.runTimes = 0;
26✔
1068
  taosMemoryFree(pStmt->sql.sqlStr);
26!
1069
  pStmt->sql.sqlStr = NULL;
26✔
1070

1071
  qDestroyQuery(pStmt->sql.pQuery);
26✔
1072
  pStmt->sql.pQuery = NULL;
26✔
1073

1074
  taosArrayDestroy(pStmt->sql.nodeList);
26✔
1075
  pStmt->sql.nodeList = NULL;
26✔
1076

1077
  taosHashCleanup(pStmt->sql.pVgHash);
26✔
1078
  pStmt->sql.pVgHash = NULL;
26✔
1079

1080
  if (pStmt->sql.fixValueTags) {
26✔
1081
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
12!
1082
    pStmt->sql.fixValueTbReq = NULL;
12✔
1083
  }
1084
  pStmt->sql.fixValueTags = false;
26✔
1085

1086
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
26✔
1087
  while (pIter) {
29✔
1088
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
3✔
1089

1090
    qDestroyStmtDataBlock(pCache->pDataCtx);
3✔
1091
    qDestroyBoundColInfo(pCache->boundTags);
3✔
1092
    taosMemoryFreeClear(pCache->boundTags);
3!
1093

1094
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
3✔
1095
  }
1096
  taosHashCleanup(pStmt->sql.pTableCache);
26✔
1097

1098
  if (pStmt->sql.stbInterlaceMode) {
26✔
1099
    pStmt->bInfo.tagsCached = false;
21✔
1100
  }
1101
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
26!
1102

1103
  if (pStmt->exec.pRequest) {
26!
1104
    taos_free_result(pStmt->exec.pRequest);
×
1105
    pStmt->exec.pRequest = NULL;
×
1106
  }
1107

1108
  if (pStmt->sql.siInfo.pTableCols) {
26✔
1109
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
22✔
1110
    pStmt->sql.siInfo.pTableCols = NULL;
22✔
1111
  }
1112

1113
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
26✔
1114
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
22✔
1115
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
22✔
1116
  }
1117

1118
  if (pStmt->sql.siInfo.pTableHash) {
26✔
1119
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
22✔
1120
    pStmt->sql.siInfo.pTableHash = NULL;
22✔
1121
  }
1122

1123
  if (pStmt->sql.siInfo.pVgroupHash) {
26!
1124
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1125
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1126
  }
1127

1128
  if (pStmt->sql.siInfo.pVgroupList) {
26!
1129
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
1130
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1131
  }
1132

1133
  if (pStmt->sql.siInfo.pDataCtx) {
26✔
1134
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
20✔
1135
    pStmt->sql.siInfo.pDataCtx = NULL;
20✔
1136
  }
1137

1138
  if (pStmt->sql.siInfo.pTSchema) {
26✔
1139
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
20!
1140
    pStmt->sql.siInfo.pTSchema = NULL;
20✔
1141
  }
1142

1143
  if (pStmt->sql.siInfo.pRequest) {
26✔
1144
    taos_free_result(pStmt->sql.siInfo.pRequest);
20✔
1145
    pStmt->sql.siInfo.pRequest = NULL;
20✔
1146
  }
1147

1148
  if (stbInterlaceMode) {
26✔
1149
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
22!
1150
  }
1151

1152
  pStmt->db = db;
26✔
1153
  pStmt->stbInterlaceMode = stbInterlaceMode;
26✔
1154
  pStmt->options = options;
26✔
1155
  pStmt->reqid = reqid;
26✔
1156

1157
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
26✔
1158
  if (NULL == pStmt->sql.pTableCache) {
26!
1159
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
1160
    return terrno;
×
1161
  }
1162

1163
  pStmt->sql.status = STMT_INIT;
26✔
1164

1165
  return TSDB_CODE_SUCCESS;
26✔
1166
}
1167

1168
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
157✔
1169
  STscStmt2* pStmt = (STscStmt2*)stmt;
157✔
1170
  int32_t    code = 0;
157✔
1171

1172
  STMT2_DLOG("start to prepare with sql:%s", sql);
157!
1173

1174
  if (stmt == NULL || sql == NULL) {
157!
1175
    STMT2_ELOG_E("stmt or sql is NULL");
×
1176
    return TSDB_CODE_INVALID_PARA;
×
1177
  }
1178

1179
  if (pStmt->sql.status >= STMT_PREPARE) {
159✔
1180
    STMT2_DLOG("stmt status is %d, need to reset stmt2 cache before prepare", pStmt->sql.status);
26!
1181
    STMT_ERR_RET(stmtDeepReset(pStmt));
26!
1182
  }
1183

1184
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
160✔
1185
    STMT2_ELOG("errCode is not success before, ErrCode: 0x%x, errorsyt: %s\n. ", pStmt->errCode,
1!
1186
               tstrerror(pStmt->errCode));
1187
    return pStmt->errCode;
1✔
1188
  }
1189

1190
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
159!
1191

1192
  if (length <= 0) {
158✔
1193
    length = strlen(sql);
113✔
1194
  }
1195

1196
  pStmt->sql.sqlStr = taosStrndup(sql, length);
158!
1197
  if (!pStmt->sql.sqlStr) {
159!
1198
    STMT2_ELOG("fail to allocate memory for sqlStr:%s", tstrerror(terrno));
×
1199
    return terrno;
×
1200
  }
1201
  pStmt->sql.sqlLen = length;
159✔
1202
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
159✔
1203

1204
  char* dbName = NULL;
159✔
1205
  if (qParseDbName(sql, length, &dbName)) {
159✔
1206
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
69!
1207
    taosMemoryFreeClear(dbName);
69!
1208
  }
1209

1210
  return TSDB_CODE_SUCCESS;
158✔
1211
}
1212

1213
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
80✔
1214
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
80✔
1215
  if (!pSrc) {
80!
1216
    return terrno;
×
1217
  }
1218
  STableDataCxt* pDst = NULL;
80✔
1219

1220
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
80!
1221
  pStmt->sql.siInfo.pDataCtx = pDst;
79✔
1222

1223
  SArray* pTblCols = NULL;
79✔
1224
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
64,620✔
1225
    pTblCols = taosArrayInit(20, POINTER_BYTES);
64,713✔
1226
    if (NULL == pTblCols) {
69,207!
1227
      return terrno;
×
1228
    }
1229

1230
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
133,748!
1231
      return terrno;
×
1232
    }
1233
  }
1234

1235
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
34✔
1236

1237
  STMT2_TLOG("init stb interlace table info, tbName:%s, pDataCtx:%p, boundTags:%p", pStmt->bInfo.tbFName,
34!
1238
             pStmt->sql.siInfo.pDataCtx, pStmt->sql.siInfo.boundTags);
1239

1240
  return TSDB_CODE_SUCCESS;
80✔
1241
}
1242

1243
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
10,754✔
1244
  STscStmt2* pStmt = (STscStmt2*)stmt;
10,754✔
1245

1246
  // STMT_DLOG_E("start is insert");
1247

1248
  if (pStmt->sql.type) {
10,754✔
1249
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
10,596✔
1250
  } else {
1251
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
158✔
1252
  }
1253

1254
  return TSDB_CODE_SUCCESS;
10,755✔
1255
}
1256

1257
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
5,343✔
1258
  STscStmt2* pStmt = (STscStmt2*)stmt;
5,343✔
1259

1260
  int64_t startUs = taosGetTimestampUs();
5,348✔
1261

1262
  STMT2_TLOG("start to set tbName:%s", tbName);
5,348!
1263

1264
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
5,347!
1265
    return pStmt->errCode;
×
1266
  }
1267

1268
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
5,347!
1269

1270
  int32_t insert = 0;
5,341✔
1271
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
5,341!
1272
  if (0 == insert) {
5,345!
1273
    STMT2_ELOG_E("set tb name not available for none insert statement");
×
1274
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1275
  }
1276

1277
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
5,345✔
1278
    STMT_ERR_RET(stmtCreateRequest(pStmt));
191!
1279

1280
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
189!
1281
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1282
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
190✔
1283

1284
    STMT_ERR_RET(stmtGetFromCache(pStmt));
187!
1285

1286
    if (pStmt->bInfo.needParse) {
189✔
1287
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
99✔
1288
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
99✔
1289

1290
      STMT_ERR_RET(stmtParseSql(pStmt));
99!
1291
    }
1292
  } else {
1293
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
5,154✔
1294
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
5,154✔
1295
    pStmt->exec.pRequest->requestId++;
5,154✔
1296
    pStmt->bInfo.needParse = false;
5,154✔
1297
  }
1298

1299
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
5,343✔
1300
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
80!
1301
  }
1302

1303
  int64_t startUs2 = taosGetTimestampUs();
5,341✔
1304
  pStmt->stat.setTbNameUs += startUs2 - startUs;
5,341✔
1305

1306
  return TSDB_CODE_SUCCESS;
5,341✔
1307
}
1308

1309
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
138✔
1310
  STscStmt2* pStmt = (STscStmt2*)stmt;
138✔
1311

1312
  STMT2_TLOG_E("start to set tbTags");
138!
1313

1314
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
138!
1315
    return pStmt->errCode;
×
1316
  }
1317

1318
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
138!
1319

1320
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
138!
1321
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1322
    pStmt->bInfo.needParse = false;
×
1323
  }
1324
  STMT_ERR_RET(stmtCreateRequest(pStmt));
138!
1325

1326
  if (pStmt->bInfo.needParse) {
138!
1327
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1328
  }
1329
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
138!
1330
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1331
  }
1332

1333
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
138✔
1334
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1335
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1336
  //   return TSDB_CODE_SUCCESS;
1337
  // }
1338
  if (pStmt->sql.autoCreateTbl && pStmt->sql.stbInterlaceMode) {
138!
1339
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
56!
1340
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1341
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
56!
1342
  }
1343

1344
  STableDataCxt** pDataBlock = NULL;
138✔
1345
  if (pStmt->exec.pCurrBlock) {
138✔
1346
    pDataBlock = &pStmt->exec.pCurrBlock;
106✔
1347
  } else {
1348
    pDataBlock =
1349
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
32✔
1350
    if (NULL == pDataBlock) {
32!
1351
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1352
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1353
    }
1354
  }
1355
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
138!
1356
    return TSDB_CODE_SUCCESS;
×
1357
  }
1358

1359
  STMT2_TLOG_E("start to bind stmt tag values");
138!
1360

1361
  void* boundTags = NULL;
138✔
1362
  if (pStmt->sql.stbInterlaceMode) {
138✔
1363
    boundTags = pStmt->sql.siInfo.boundTags;
56✔
1364
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
56!
1365
    if (NULL == pCreateTbReq) {
56!
1366
      return terrno;
×
1367
    }
1368
    int32_t vgId = -1;
56✔
1369
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
56!
1370
    (*pCreateTbReq)->uid = vgId;
56✔
1371
  } else {
1372
    boundTags = pStmt->bInfo.boundTags;
82✔
1373
  }
1374

1375
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
138✔
1376
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1377
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1378

1379
  return TSDB_CODE_SUCCESS;
137✔
1380
}
1381

1382
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
26✔
1383
  STscStmt2* pStmt = (STscStmt2*)stmt;
26✔
1384

1385
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
26!
1386

1387
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
26!
1388
    return pStmt->errCode;
×
1389
  }
1390

1391
  if (!pStmt->sql.stbInterlaceMode) {
26!
1392
    return TSDB_CODE_SUCCESS;
×
1393
  }
1394

1395
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
26!
1396

1397
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
26!
1398
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1399
    pStmt->bInfo.needParse = false;
×
1400
  }
1401
  STMT_ERR_RET(stmtCreateRequest(pStmt));
26!
1402

1403
  if (pStmt->bInfo.needParse) {
26!
1404
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1405
    if (!pStmt->sql.autoCreateTbl) {
×
1406
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1407
      return TSDB_CODE_SUCCESS;
×
1408
    }
1409
  }
1410

1411
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
26!
1412
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1413
  }
1414

1415
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
26!
1416
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1417
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
26!
1418

1419
  STableDataCxt** pDataBlock = NULL;
26✔
1420
  if (pStmt->exec.pCurrBlock) {
26✔
1421
    pDataBlock = &pStmt->exec.pCurrBlock;
9✔
1422
  } else {
1423
    pDataBlock =
1424
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
17✔
1425
    if (NULL == pDataBlock) {
17!
1426
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1427
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1428
    }
1429
  }
1430

1431
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
26!
1432
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1433
    return TSDB_CODE_SUCCESS;
×
1434
  }
1435

1436
  if (pStmt->sql.fixValueTags) {
26✔
1437
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
9!
1438
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
9!
1439
    if ((*pCreateTbReq)->name) {
9!
1440
      taosMemoryFree((*pCreateTbReq)->name);
9!
1441
    }
1442
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
9!
1443
    int32_t vgId = -1;
9✔
1444
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
9!
1445
    (*pCreateTbReq)->uid = vgId;
9✔
1446
    return TSDB_CODE_SUCCESS;
9✔
1447
  }
1448

1449
  if ((*pDataBlock)->pData->pCreateTbReq) {
17!
1450
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
17!
1451
    pStmt->sql.fixValueTags = true;
17✔
1452
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
17!
1453
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
17!
1454
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
17✔
1455
  }
1456

1457
  return TSDB_CODE_SUCCESS;
17✔
1458
}
1459

1460
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1461
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1462
    return pStmt->errCode;
×
1463
  }
1464

1465
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1466
    tscError("invalid operation to get query column fileds");
×
1467
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1468
  }
1469

1470
  STableDataCxt** pDataBlock = NULL;
×
1471

1472
  if (pStmt->sql.stbInterlaceMode) {
×
1473
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1474
  } else {
1475
    pDataBlock =
1476
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1477
    if (NULL == pDataBlock) {
×
1478
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1479
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1480
    }
1481
  }
1482

1483
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1484

1485
  return TSDB_CODE_SUCCESS;
×
1486
}
1487

1488
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
42✔
1489
  int32_t code = 0;
42✔
1490
  int32_t preCode = pStmt->errCode;
42✔
1491

1492
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
42!
1493
    return pStmt->errCode;
×
1494
  }
1495

1496
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
42!
1497
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1498
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1499
  }
1500

1501
  STableDataCxt** pDataBlock = NULL;
42✔
1502
  bool            cleanStb = false;
42✔
1503

1504
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
42✔
1505
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
7✔
1506
  } else {
1507
    cleanStb = true;
35✔
1508
    pDataBlock =
1509
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
35✔
1510
  }
1511

1512
  if (NULL == pDataBlock || NULL == *pDataBlock) {
42!
1513
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1514
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1515
  }
1516

1517
  STMT_ERRI_JRET(
42!
1518
      qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbNameFlag, fieldNum, fields));
1519

1520
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
42!
1521
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
28!
1522
    qDestroyStmtDataBlock(*pDataBlock);
28✔
1523
    *pDataBlock = NULL;
28✔
1524
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
28!
1525
      STMT2_ELOG("fail to remove remove stb:%s exec blockHash", pStmt->bInfo.tbFName);
×
1526
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1527
    }
1528
    pStmt->sql.autoCreateTbl = false;
28✔
1529
    pStmt->bInfo.tagsCached = false;
28✔
1530
    pStmt->bInfo.sname = (SName){0};
28✔
1531
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
28!
1532
  }
1533

1534
_return:
14✔
1535

1536
  pStmt->errCode = preCode;
42✔
1537

1538
  return code;
42✔
1539
}
1540
/*
1541
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1542
  while (true) {
1543
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1544
      pStmt->exec.smInfo.pColIdx = 0;
1545
    }
1546

1547
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1548
      taosUsleep(1);
1549
      continue;
1550
    }
1551

1552
    *idx = pStmt->exec.smInfo.pColIdx;
1553
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1554
  }
1555
}
1556
*/
1557
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
5,230✔
1558
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,230✔
1559
    pStmt->sql.siInfo.pVgroupHash =
4,190✔
1560
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,188✔
1561
  }
1562
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,232✔
1563
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,191✔
1564
  }
1565

1566
  if (NULL == pStmt->sql.siInfo.pRequest) {
5,231✔
1567
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
77✔
1568
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1569

1570
    if (pStmt->reqid != 0) {
77!
1571
      pStmt->reqid++;
×
1572
    }
1573
    pStmt->exec.pRequest->syncQuery = true;
77✔
1574

1575
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
77✔
1576
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
77✔
1577
  }
1578

1579
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
5,231✔
1580
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
72✔
1581
    pStmt->sql.siInfo.tbFromHash = true;
36✔
1582
  }
1583

1584
  if (0 == pStmt->sql.siInfo.firstName[0]) {
5,231✔
1585
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
58✔
1586
  }
1587

1588
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,231✔
1589
  param->next = NULL;
5,231✔
1590

1591
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,231✔
1592

1593
  if (pStmt->queue.stopQueue) {
5,245!
1594
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1595
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1596
  }
1597
  stmtEnqueue(pStmt, param);
5,245✔
1598

1599
  return TSDB_CODE_SUCCESS;
5,240✔
1600
}
1601

1602
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1603
  while (true) {
1604
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
5,234!
1605
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,232✔
1606
      break;
5,233✔
1607
    } else {
1608
      SArray* pTblCols = NULL;
×
1609
      for (int32_t i = 0; i < 100; i++) {
×
1610
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1611
        if (NULL == pTblCols) {
×
1612
          return terrno;
×
1613
        }
1614

1615
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1616
          return terrno;
×
1617
        }
1618
      }
1619
    }
1620
  }
1621

1622
  return TSDB_CODE_SUCCESS;
5,233✔
1623
}
1624

1625
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
116✔
1626
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
116✔
1627
    return TSDB_CODE_SUCCESS;
9✔
1628
  }
1629

1630
  uint64_t uid = pStmt->bInfo.tbUid;
107✔
1631
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
107!
1632

1633
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
107✔
1634
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
89!
1635
    return TSDB_CODE_SUCCESS;
89✔
1636
  }
1637

1638
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
18✔
1639
  if (!pSrc) {
18!
1640
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1641
    return terrno;
×
1642
  }
1643
  STableDataCxt* pDst = NULL;
18✔
1644

1645
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
18!
1646

1647
  SStmtTableCache cache = {
18✔
1648
      .pDataCtx = pDst,
1649
      .boundTags = pStmt->bInfo.boundTags,
18✔
1650
  };
1651

1652
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
18!
1653
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
1654
    return terrno;
×
1655
  }
1656

1657
  if (pStmt->sql.autoCreateTbl) {
18✔
1658
    pStmt->bInfo.tagsCached = true;
15✔
1659
  } else {
1660
    pStmt->bInfo.boundTags = NULL;
3✔
1661
  }
1662

1663
  return TSDB_CODE_SUCCESS;
18✔
1664
}
1665

1666
static int stmtAddBatch2(TAOS_STMT2* stmt) {
4,310✔
1667
  STscStmt2* pStmt = (STscStmt2*)stmt;
4,310✔
1668

1669
  int64_t startUs = taosGetTimestampUs();
4,310✔
1670

1671
  // STMT2_TLOG_E("start to add batch");
1672

1673
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4,310!
1674
    return pStmt->errCode;
×
1675
  }
1676

1677
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
4,310!
1678

1679
  if (pStmt->sql.stbInterlaceMode) {
4,306✔
1680
    int64_t startUs2 = taosGetTimestampUs();
4,189✔
1681
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,189✔
1682

1683
    pStmt->sql.siInfo.tableColsReady = false;
4,189✔
1684

1685
    SStmtQNode* param = NULL;
4,189✔
1686
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
8,380!
1687
    param->restoreTbCols = true;
4,191✔
1688
    param->next = NULL;
4,191✔
1689

1690
    if (pStmt->sql.autoCreateTbl) {
4,191✔
1691
      pStmt->bInfo.tagsCached = true;
45✔
1692
    }
1693

1694
    if (pStmt->queue.stopQueue) {
4,191!
1695
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1696
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1697
    }
1698

1699
    stmtEnqueue(pStmt, param);
4,191✔
1700

1701
    return TSDB_CODE_SUCCESS;
4,195✔
1702
  }
1703

1704
  STMT_ERR_RET(stmtCacheBlock(pStmt));
116!
1705

1706
  return TSDB_CODE_SUCCESS;
116✔
1707
}
1708
/*
1709
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1710
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1711
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1712
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1713

1714
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1715
  pRes->fields = taosMemoryMalloc(size);
1716
  pRes->userFields = taosMemoryMalloc(size);
1717
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1718
    STMT_ERR_RET(terrno);
1719
  }
1720
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1721
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1722

1723
  return TSDB_CODE_SUCCESS;
1724
}
1725

1726
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1727
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1728
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1729

1730
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1731
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1732

1733
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1734
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1735
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1736
      STMT_ERR_RET(terrno);
1737
    }
1738
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1739
  }
1740

1741
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1742
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1743
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1744
      STMT_ERR_RET(terrno);
1745
    }
1746
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1747
  }
1748

1749
  return TSDB_CODE_SUCCESS;
1750
}
1751
*/
1752
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
5,356✔
1753
  STscStmt2* pStmt = (STscStmt2*)stmt;
5,356✔
1754
  int32_t    code = 0;
5,356✔
1755

1756
  int64_t startUs = taosGetTimestampUs();
5,357✔
1757

1758
  STMT2_TLOG("start to bind data, colIdx:%d", colIdx);
5,357!
1759

1760
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
5,358!
1761
    return pStmt->errCode;
×
1762
  }
1763

1764
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
5,358!
1765

1766
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
5,355!
1767
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1768
    pStmt->bInfo.needParse = false;
×
1769
  }
1770

1771
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
5,355✔
1772
    taos_free_result(pStmt->exec.pRequest);
1✔
1773
    pStmt->exec.pRequest = NULL;
1✔
1774
  }
1775

1776
  STMT_ERR_RET(stmtCreateRequest(pStmt));
5,355!
1777
  if (pStmt->bInfo.needParse) {
5,353✔
1778
    code = stmtParseSql(pStmt);
6✔
1779
    if (code != TSDB_CODE_SUCCESS) {
6✔
1780
      goto cleanup_root;
2✔
1781
    }
1782
  }
1783

1784
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
5,351✔
1785
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
5✔
1786
    if (code != TSDB_CODE_SUCCESS) {
5!
1787
      goto cleanup_root;
×
1788
    }
1789
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
5✔
1790
                         .acctId = pStmt->taos->acctId,
5✔
1791
                         .db = pStmt->exec.pRequest->pDb,
5✔
1792
                         .topicQuery = false,
1793
                         .pSql = pStmt->sql.sqlStr,
5✔
1794
                         .sqlLen = pStmt->sql.sqlLen,
5✔
1795
                         .pMsg = pStmt->exec.pRequest->msgBuf,
5✔
1796
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1797
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
5✔
1798
                         .pStmtCb = NULL,
1799
                         .pUser = pStmt->taos->user};
5✔
1800
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
5✔
1801
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
5✔
1802
    if (code != TSDB_CODE_SUCCESS) {
5!
1803
      goto cleanup_root;
×
1804
    }
1805
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
5✔
1806
    if (code != TSDB_CODE_SUCCESS) {
5!
1807
      goto cleanup_root;
×
1808
    }
1809

1810
    if (pStmt->sql.pQuery->haveResultSet) {
5!
1811
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
12!
1812
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1813
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
5!
1814
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
5!
1815
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
5✔
1816
    }
1817

1818
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
5✔
1819
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
5✔
1820
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
5✔
1821

1822
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1823
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1824
    // }
1825

1826
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1827

1828
    return TSDB_CODE_SUCCESS;
5✔
1829

1830
  cleanup_root:
2✔
1831
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
2!
1832
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
2!
1833
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
1✔
1834
      pStmt->sql.pQuery->pRoot = NULL;
1✔
1835
    }
1836
    STMT_ERR_RET(code);
2!
1837
  }
1838

1839
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
5,346!
1840
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1841
  }
1842

1843
  STableDataCxt** pDataBlock = NULL;
5,345✔
1844

1845
  if (pStmt->exec.pCurrBlock) {
5,345✔
1846
    pDataBlock = &pStmt->exec.pCurrBlock;
5,245✔
1847
  } else {
1848
    pDataBlock =
1849
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
100✔
1850
    if (NULL == pDataBlock) {
101!
1851
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1852
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1853
    }
1854
    pStmt->exec.pCurrBlock = *pDataBlock;
101✔
1855
    if (pStmt->sql.stbInterlaceMode) {
101✔
1856
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
79✔
1857
      (*pDataBlock)->pData->aCol = NULL;
79✔
1858
    }
1859
    if (colIdx < -1) {
101✔
1860
      pStmt->sql.bindRowFormat = true;
1✔
1861
      taosArrayDestroy((*pDataBlock)->pData->aCol);
1✔
1862
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
1✔
1863
    }
1864
  }
1865

1866
  int64_t startUs2 = taosGetTimestampUs();
5,348✔
1867
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
5,348✔
1868

1869
  SStmtQNode* param = NULL;
5,348✔
1870
  if (pStmt->sql.stbInterlaceMode) {
5,348✔
1871
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
10,465!
1872
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
10,467!
1873
    taosArrayClear(param->tblData.aCol);
5,233✔
1874

1875
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1876

1877
    param->restoreTbCols = false;
5,228✔
1878
    param->tblData.isOrdered = true;
5,228✔
1879
    param->tblData.isDuplicateTs = false;
5,228✔
1880
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,228✔
1881

1882
    param->pCreateTbReq = pCreateTbReq;
5,228✔
1883
  }
1884

1885
  int64_t startUs3 = taosGetTimestampUs();
5,349✔
1886
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
5,349✔
1887

1888
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
5,349✔
1889

1890
  if (colIdx < 0) {
5,349✔
1891
    if (pStmt->sql.stbInterlaceMode) {
5,348✔
1892
      // (*pDataBlock)->pData->flags = 0;
1893
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
5,236✔
1894
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
5,236✔
1895
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
5,236✔
1896
                                    pStmt->taos->optionInfo.charsetCxt);
5,236✔
1897
      param->tblData.isOrdered = (*pDataBlock)->ordered;
5,231✔
1898
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
5,231✔
1899
    } else {
1900
      if (colIdx == -1) {
112✔
1901
        if (pStmt->sql.bindRowFormat) {
109✔
1902
          STMT2_ELOG_E("can't mix bind row format and bind column format");
1!
1903
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
1!
1904
        }
1905
        code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
108✔
1906
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
108✔
1907
      } else {
1908
        code = qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, bind, pStmt->exec.pRequest->msgBuf,
3✔
1909
                                  pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
3✔
1910
                                  pStmt->taos->optionInfo.charsetCxt);
3✔
1911
      }
1912
    }
1913

1914
    if (code) {
5,341✔
1915
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
1!
1916
      STMT_ERR_RET(code);
1!
1917
    }
1918
  } else {
1919
    if (pStmt->sql.stbInterlaceMode) {
6!
1920
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
1921
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1922
    }
1923

1924
    if (pStmt->sql.bindRowFormat) {
6!
1925
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
1926
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1927
    }
1928

1929
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
6!
1930
      STMT2_ELOG_E("bind column index not in sequence");
×
1931
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1932
    }
1933

1934
    pStmt->bInfo.sBindLastIdx = colIdx;
6✔
1935

1936
    if (0 == colIdx) {
6✔
1937
      pStmt->bInfo.sBindRowNum = bind->num;
3✔
1938
    }
1939

1940
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
6✔
1941
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
6✔
1942
                                    pStmt->taos->optionInfo.charsetCxt);
6✔
1943
    if (code) {
6!
1944
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
1945
      STMT_ERR_RET(code);
×
1946
    }
1947
  }
1948

1949
  int64_t startUs4 = taosGetTimestampUs();
5,342✔
1950
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
5,342✔
1951

1952
  if (pStmt->sql.stbInterlaceMode) {
5,342✔
1953
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,234!
1954
  } else {
1955
    STMT_ERR_RET(stmtAddBatch2(pStmt));
116!
1956
  }
1957

1958
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
5,358✔
1959

1960
  return TSDB_CODE_SUCCESS;
5,358✔
1961
}
1962
/*
1963
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1964
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1965

1966
  int32_t code = 0;
1967
  int32_t finalCode = 0;
1968
  size_t  keyLen = 0;
1969
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1970
  while (pIter) {
1971
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1972
    char*          key = taosHashGetKey(pIter, &keyLen);
1973

1974
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1975
    if (pMeta->uid) {
1976
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1977
      continue;
1978
    }
1979

1980
    SSubmitBlkRsp* blkRsp = NULL;
1981
    int32_t        i = 0;
1982
    for (; i < pRsp->nBlocks; ++i) {
1983
      blkRsp = pRsp->pBlocks + i;
1984
      if (strlen(blkRsp->tblFName) != keyLen) {
1985
        continue;
1986
      }
1987

1988
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1989
        continue;
1990
      }
1991

1992
      break;
1993
    }
1994

1995
    if (i < pRsp->nBlocks) {
1996
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1997
               blkRsp->uid);
1998

1999
      pMeta->uid = blkRsp->uid;
2000
      pStmt->bInfo.tbUid = blkRsp->uid;
2001
    } else {
2002
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2003
      if (NULL == pStmt->pCatalog) {
2004
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2005
        if (code) {
2006
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2007
          finalCode = code;
2008
          continue;
2009
        }
2010
      }
2011

2012
      code = stmtCreateRequest(pStmt);
2013
      if (code) {
2014
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2015
        finalCode = code;
2016
        continue;
2017
      }
2018

2019
      STableMeta*      pTableMeta = NULL;
2020
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2021
                               .requestId = pStmt->exec.pRequest->requestId,
2022
                               .requestObjRefId = pStmt->exec.pRequest->self,
2023
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2024
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2025

2026
      pStmt->stat.ctgGetTbMetaNum++;
2027

2028
      taos_free_result(pStmt->exec.pRequest);
2029
      pStmt->exec.pRequest = NULL;
2030

2031
      if (code || NULL == pTableMeta) {
2032
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2033
        finalCode = code;
2034
        taosMemoryFree(pTableMeta);
2035
        continue;
2036
      }
2037

2038
      pMeta->uid = pTableMeta->uid;
2039
      pStmt->bInfo.tbUid = pTableMeta->uid;
2040
      taosMemoryFree(pTableMeta);
2041
    }
2042

2043
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2044
  }
2045

2046
  return finalCode;
2047
}
2048
*/
2049
/*
2050
int stmtStaticModeExec(TAOS_STMT* stmt) {
2051
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2052
  int32_t     code = 0;
2053
  SSubmitRsp* pRsp = NULL;
2054
  if (pStmt->sql.staticMode) {
2055
    return TSDB_CODE_TSC_STMT_API_ERROR;
2056
  }
2057

2058
  STMT_DLOG_E("start to exec");
2059

2060
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2061

2062
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2063
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2064

2065
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2066

2067
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2068
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2069
    if (code) {
2070
      pStmt->exec.pRequest->code = code;
2071
    } else {
2072
      tFreeSSubmitRsp(pRsp);
2073
      STMT_ERR_RET(stmtResetStmt(pStmt));
2074
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2075
    }
2076
  }
2077

2078
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2079

2080
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2081
  pStmt->affectedRows += pStmt->exec.affectedRows;
2082

2083
_return:
2084

2085
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2086

2087
  tFreeSSubmitRsp(pRsp);
2088

2089
  ++pStmt->sql.runTimes;
2090

2091
  STMT_RET(code);
2092
}
2093
*/
2094

2095
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
15✔
2096
  const STscObj* pTscObj = pRequest->pTscObj;
15✔
2097

2098
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
15!
2099
  if (*pCxt == NULL) {
15!
2100
    return terrno;
×
2101
  }
2102

2103
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
15✔
2104
                           .requestRid = pRequest->self,
15✔
2105
                           .acctId = pTscObj->acctId,
15✔
2106
                           .db = pRequest->pDb,
15✔
2107
                           .topicQuery = false,
2108
                           .pSql = pRequest->sqlstr,
15✔
2109
                           .sqlLen = pRequest->sqlLen,
15✔
2110
                           .pMsg = pRequest->msgBuf,
15✔
2111
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2112
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
15✔
2113
                           .pStmtCb = NULL,
2114
                           .pUser = pTscObj->user,
15✔
2115
                           .pEffectiveUser = pRequest->effectiveUser,
15✔
2116
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
15✔
2117
                           .enableSysInfo = pTscObj->sysInfo,
15✔
2118
                           .async = true,
2119
                           .svrVer = pTscObj->sVer,
15✔
2120
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
15✔
2121
                           .allocatorId = pRequest->allocatorRefId,
15✔
2122
                           .parseSqlFp = clientParseSql,
2123
                           .parseSqlParam = pWrapper};
2124
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
15✔
2125
  (*pCxt)->biMode = biMode;
15✔
2126
  return TSDB_CODE_SUCCESS;
15✔
2127
}
2128

2129
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
15✔
2130
  STscStmt2*        pStmt = userdata;
15✔
2131
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
15✔
2132
  pStmt->asyncExecCb = true;
15✔
2133

2134
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
15✔
2135
  pStmt->affectedRows += pStmt->exec.affectedRows;
15✔
2136

2137
  fp(pStmt->options.userdata, res, code);
15✔
2138

2139
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
15!
2140
    taosUsleep(1);
×
2141
  }
2142
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
15✔
2143
  ++pStmt->sql.runTimes;
15✔
2144

2145
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
15!
2146
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2147
  }
2148
}
15✔
2149

2150
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
4,242✔
2151
  STscStmt2* pStmt = (STscStmt2*)stmt;
4,242✔
2152
  int32_t    code = 0;
4,242✔
2153
  int64_t    startUs = taosGetTimestampUs();
4,242✔
2154

2155
  STMT2_DLOG_E("start to exec");
4,242!
2156

2157
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4,243!
2158
    return pStmt->errCode;
×
2159
  }
2160

2161
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
4,243!
2162
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
4,248!
2163
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2164
  }
2165
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
4,246!
2166

2167
  if (pStmt->sql.stbInterlaceMode) {
4,249✔
2168
    STMT_ERR_RET(stmtAddBatch2(pStmt));
4,196!
2169
  }
2170

2171
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
4,248✔
2172
  pStmt->asyncExecCb = false;
4,248✔
2173

2174
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
4,248✔
2175
    if (pStmt->sql.stbInterlaceMode) {
4,242✔
2176
      int64_t startTs = taosGetTimestampUs();
4,196✔
2177
      // wait for stmt bind thread to finish
2178
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
8,654✔
2179
        taosUsleep(1);
4,464✔
2180
      }
2181

2182
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,188✔
2183
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,188!
2184
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,192✔
2185
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,194✔
2186
      pStmt->sql.siInfo.pVgroupList = NULL;
4,194✔
2187
    } else {
2188
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
47✔
2189
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
47!
2190

2191
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
47!
2192

2193
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
47!
2194
    }
2195
  }
2196

2197
  SRequestObj*      pRequest = pStmt->exec.pRequest;
4,242✔
2198
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
4,242✔
2199

2200
  if (!fp) {
4,242✔
2201
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
4,227✔
2202

2203
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
4,228!
2204
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2205
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2206
      if (code) {
×
2207
        pStmt->exec.pRequest->code = code;
×
2208

2209
      } else {
2210
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2211
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2212
      }
2213
    }
2214

2215
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
4,228!
2216

2217
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
4,228✔
2218
    if (affected_rows) {
4,231✔
2219
      *affected_rows = pStmt->exec.affectedRows;
4,223✔
2220
    }
2221
    pStmt->affectedRows += pStmt->exec.affectedRows;
4,231✔
2222

2223
    // wait for stmt bind thread to finish
2224
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
4,231!
2225
      taosUsleep(1);
×
2226
    }
2227

2228
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
4,229✔
2229

2230
    ++pStmt->sql.runTimes;
4,226✔
2231
  } else {
2232
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
15!
2233
    if (pWrapper == NULL) {
15!
2234
      code = terrno;
×
2235
    } else {
2236
      pWrapper->pRequest = pRequest;
15✔
2237
      pRequest->pWrapper = pWrapper;
15✔
2238
    }
2239
    if (TSDB_CODE_SUCCESS == code) {
15!
2240
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
15✔
2241
    }
2242
    pRequest->syncQuery = false;
15✔
2243
    pRequest->body.queryFp = asyncQueryCb;
15✔
2244
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
15✔
2245

2246
    pStmt->execSemWaited = false;
15✔
2247
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
15✔
2248
  }
2249

2250
_return:
4,241✔
2251
  if (code) {
4,241!
2252
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2253
  }
2254
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
4,240✔
2255

2256
  STMT_RET(code);
4,240!
2257
}
2258

2259
int stmtClose2(TAOS_STMT2* stmt) {
134✔
2260
  STscStmt2* pStmt = (STscStmt2*)stmt;
134✔
2261

2262
  STMT2_DLOG_E("start to close stmt");
134!
2263
  taosMemoryFreeClear(pStmt->db);
134!
2264

2265
  if (pStmt->bindThreadInUse) {
134✔
2266
    // wait for stmt bind thread to finish
2267
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
69!
2268
      taosUsleep(1);
×
2269
    }
2270

2271
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
69✔
2272
    pStmt->queue.stopQueue = true;
69✔
2273
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
69✔
2274
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
69✔
2275

2276
    (void)taosThreadJoin(pStmt->bindThread, NULL);
69✔
2277
    pStmt->bindThreadInUse = false;
69✔
2278

2279
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
69✔
2280
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
69✔
2281
  }
2282

2283
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
134!
2284
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
134!
2285
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2286
  }
2287
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
134!
2288

2289
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
134✔
2290
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
134✔
2291

2292
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
134!
2293
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
6!
2294
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2295
    }
2296
  }
2297

2298
  STMT2_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
134!
2299
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2300
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2301
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2302
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2303
             pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2304
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2305
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2306
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2307
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2308
  if (pStmt->sql.stbInterlaceMode) {
134✔
2309
    pStmt->bInfo.tagsCached = false;
63✔
2310
  }
2311

2312
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
134!
2313

2314
  if (pStmt->options.asyncExecFn) {
134✔
2315
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
6!
2316
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2317
    }
2318
  }
2319
  taosMemoryFree(stmt);
134!
2320

2321
  return TSDB_CODE_SUCCESS;
134✔
2322
}
2323

2324
const char* stmtErrstr2(TAOS_STMT2* stmt) {
5✔
2325
  STscStmt2* pStmt = (STscStmt2*)stmt;
5✔
2326

2327
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
5!
2328
    return (char*)tstrerror(terrno);
3✔
2329
  }
2330

2331
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2332
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncExecCb)) {
2!
2333
    pStmt->exec.pRequest->code = terrno;
2✔
2334
  }
2335

2336
  SRequestObj* pRequest = pStmt->exec.pRequest;
2✔
2337
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
2!
2338
    return pRequest->msgBuf;
2✔
2339
  }
2340
  return (const char*)tstrerror(pRequest->code);
×
2341
}
2342
/*
2343
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2344

2345
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2346
*/
2347

2348
int stmtParseColFields2(TAOS_STMT2* stmt) {
57✔
2349
  int32_t    code = 0;
57✔
2350
  STscStmt2* pStmt = (STscStmt2*)stmt;
57✔
2351
  int32_t    preCode = pStmt->errCode;
57✔
2352

2353
  STMT2_DLOG_E("start to get col fields for insert");
57!
2354

2355
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
57!
2356
    return pStmt->errCode;
×
2357
  }
2358

2359
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
57!
2360
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2361
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2362
  }
2363

2364
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
57!
2365

2366
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
57!
2367
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
3!
2368
    pStmt->bInfo.needParse = false;
×
2369
  }
2370
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
57✔
2371
    pStmt->bInfo.needParse = false;
7✔
2372
  }
2373

2374
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
57!
2375

2376
  if (pStmt->bInfo.needParse) {
57✔
2377
    STMT_ERRI_JRET(stmtParseSql(pStmt));
50✔
2378
  }
2379

2380
_return:
42✔
2381
  // compatible with previous versions
2382
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
57!
2383
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
2384
  }
2385

2386
  if (code != TSDB_CODE_SUCCESS) {
57✔
2387
    STMT2_ELOG("stmt get fileds parse failed, code:%d", code);
15!
2388
    taos_free_result(pStmt->exec.pRequest);
15✔
2389
    pStmt->exec.pRequest = NULL;
15✔
2390
  }
2391

2392
  pStmt->errCode = preCode;
57✔
2393

2394
  return code;
57✔
2395
}
2396

2397
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
57✔
2398
  int32_t code = stmtParseColFields2(stmt);
57✔
2399
  if (code != TSDB_CODE_SUCCESS) {
57✔
2400
    return code;
15✔
2401
  }
2402

2403
  return stmtFetchStbColFields2(stmt, nums, fields);
42✔
2404
}
2405

2406
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
13✔
2407
  int32_t    code = 0;
13✔
2408
  STscStmt2* pStmt = (STscStmt2*)stmt;
13✔
2409
  int32_t    preCode = pStmt->errCode;
13✔
2410

2411
  STMT2_DLOG_E("start to get param num for query");
13!
2412

2413
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
13!
2414
    return pStmt->errCode;
×
2415
  }
2416

2417
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
13!
2418

2419
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
13!
2420
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2421
    pStmt->bInfo.needParse = false;
×
2422
  }
2423

2424
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
13!
2425
    taos_free_result(pStmt->exec.pRequest);
×
2426
    pStmt->exec.pRequest = NULL;
×
2427
  }
2428

2429
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
13!
2430

2431
  if (pStmt->bInfo.needParse) {
13!
2432
    STMT_ERRI_JRET(stmtParseSql(pStmt));
13✔
2433
  }
2434

2435
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6!
2436
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
6✔
2437
  } else {
2438
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2439
  }
2440

2441
  STMT2_TLOG("get param num success, nums:%d", *nums);
6!
2442

2443
_return:
6✔
2444
  if (code != TSDB_CODE_SUCCESS) {
13✔
2445
    taos_free_result(pStmt->exec.pRequest);
7✔
2446
    pStmt->exec.pRequest = NULL;
7✔
2447
  }
2448
  pStmt->errCode = preCode;
13✔
2449

2450
  return code;
13✔
2451
}
2452

2453
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
5✔
2454
  STscStmt2* pStmt = (STscStmt2*)stmt;
5✔
2455

2456
  STMT2_TLOG_E("start to use result");
5!
2457

2458
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
5!
2459
    STMT2_ELOG_E("useResult only for query statement");
×
2460
    return NULL;
×
2461
  }
2462

2463
  return pStmt->exec.pRequest;
5✔
2464
}
2465

2466
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2467
  qInfo("async stmt bind thread started");
×
2468

2469
  ThreadArgs* targs = (ThreadArgs*)args;
×
2470
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2471

2472
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2473
  targs->fp(targs->param, NULL, code);
×
2474
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2475
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2476
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2477
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2478
  taosMemoryFree(args);
×
2479

2480
  qInfo("async stmt bind thread stopped");
×
2481

2482
  return code;
×
2483
}
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