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

taosdata / TDengine / #3663

19 Mar 2025 09:21AM UTC coverage: 61.664% (-0.6%) from 62.28%
#3663

push

travis-ci

web-flow
docs: add defination of tmq_config_res_t & fix spell error (#30271)

153169 of 318241 branches covered (48.13%)

Branch coverage included in aggregate %.

239405 of 318390 relevant lines covered (75.19%)

5762846.6 hits per line

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

33.93
/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) {
829✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
829✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
829✔
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;
829✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
829✔
42
  int i = 0;
829✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
3,617✔
44
    if (i < 10) {
2,792✔
45
      taosUsleep(1);
2,662✔
46
      i++;
2,659✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
130✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
129!
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
129✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
129✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
825✔
56
    return false;
24✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
801✔
59
  SStmtQNode* node = pStmt->queue.head->next;
801✔
60
  pStmt->queue.head = pStmt->queue.head->next;
801✔
61
  *param = node;
801✔
62

63
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
801✔
64

65
  return true;
806✔
66
}
67

68
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
806✔
69
  pStmt->queue.tail->next = param;
806✔
70
  pStmt->queue.tail = param;
806✔
71

72
  pStmt->stat.bindDataNum++;
806✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
806✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
806✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
806✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
806✔
78
}
806✔
79

80
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
599✔
81
  int32_t code = 0;
599✔
82

83
  if (pStmt->exec.pRequest == NULL) {
599✔
84
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
24✔
85
                        pStmt->reqid);
86
    if (pStmt->reqid != 0) {
24!
87
      pStmt->reqid++;
×
88
    }
89
    if (pStmt->db != NULL) {
24!
90
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb); 
×
91
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
×
92
    }
93
    if (TSDB_CODE_SUCCESS == code) {
24!
94
      pStmt->exec.pRequest->syncQuery = true;
24✔
95
      pStmt->exec.pRequest->isStmtBind = true;
24✔
96
    }
97
  }
98

99
  return code;
599✔
100
}
101

102
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
1,634✔
103
  int32_t code = 0;
1,634✔
104

105
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
1,634!
106
    STMT_LOG_SEQ(newStatus);
1,634!
107
  }
108

109
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
1,633!
110
    STMT_DLOG("stmt already failed with err:%s", tstrerror(pStmt->errCode));
×
111
    return pStmt->errCode;
×
112
  }
113

114
  switch (newStatus) {
1,633!
115
    case STMT_PREPARE:
23✔
116
      pStmt->errCode = 0;
23✔
117
      break;
23✔
118
    case STMT_SETTBNAME:
553✔
119
      if (STMT_STATUS_EQ(INIT)) {
553!
120
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
121
      }
122
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
553!
123
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
124
      }
125
      break;
553✔
126
    case STMT_SETTAGS:
×
127
      if (STMT_STATUS_EQ(INIT)) {
×
128
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
129
      }
130
      break;
×
131
    case STMT_FETCH_FIELDS:
×
132
      if (STMT_STATUS_EQ(INIT)) {
×
133
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
134
      }
135
      break;
×
136
    case STMT_BIND:
551✔
137
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
551!
138
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
139
      }
140
      /*
141
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
142
              code = TSDB_CODE_TSC_STMT_API_ERROR;
143
            }
144
      */
145
      break;
551✔
146
    case STMT_BIND_COL:
×
147
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
148
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
149
      }
150
      break;
×
151
    case STMT_ADD_BATCH:
253✔
152
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
253!
153
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
154
      }
155
      break;
253✔
156
    case STMT_EXECUTE:
253✔
157
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
253!
158
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
×
159
            STMT_STATUS_NE(BIND_COL)) {
×
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      } else {
163
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
253!
164
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
165
        }
166
      }
167
      break;
253✔
168
    default:
×
169
      code = TSDB_CODE_APP_ERROR;
×
170
      break;
×
171
  }
172

173
  STMT_ERR_RET(code);
1,633!
174

175
  pStmt->sql.status = newStatus;
1,633✔
176

177
  return TSDB_CODE_SUCCESS;
1,633✔
178
}
179

180
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
24✔
181
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
182

183
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
24✔
184

185
  if ('\0' == pStmt->bInfo.tbName[0]) {
24!
186
    tscWarn("no table name set, OK if it is a stmt get fields");
×
187
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
×
188
  }
189

190
  *tbName = pStmt->bInfo.tbName;
23✔
191

192
  return TSDB_CODE_SUCCESS;
23✔
193
}
194

195
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName,
22✔
196
                                  const char* sTableName, bool autoCreateTbl, bool preCtbname) {
197
  STscStmt2* pStmt = (STscStmt2*)stmt;
22✔
198
  char       tbFName[TSDB_TABLE_FNAME_LEN];
199
  int32_t    code = tNameExtractFullName(tbName, tbFName);
22✔
200
  if (code != 0) {
24!
201
    return code;
×
202
  }
203

204
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
24✔
205
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
24✔
206
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
24✔
207

208
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
24!
209
  pStmt->bInfo.tbSuid = pTableMeta->suid;
24✔
210
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
24✔
211
  pStmt->bInfo.tbType = pTableMeta->tableType;
24✔
212

213
  if (!pStmt->bInfo.tagsCached) {
24✔
214
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
22✔
215
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
21!
216
  }
217

218
  pStmt->bInfo.boundTags = tags;
20✔
219
  pStmt->bInfo.tagsCached = false;
20✔
220
  pStmt->bInfo.preCtbname = preCtbname;
20✔
221
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
20✔
222

223
  return TSDB_CODE_SUCCESS;
20✔
224
}
225

226
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
20✔
227
  STscStmt2* pStmt = (STscStmt2*)stmt;
20✔
228

229
  pStmt->sql.pVgHash = pVgHash;
20✔
230
  pStmt->exec.pBlockHash = pBlockHash;
20✔
231

232
  return TSDB_CODE_SUCCESS;
20✔
233
}
234

235
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
23✔
236
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
237
  STscStmt2* pStmt = (STscStmt2*)stmt;
23✔
238

239
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, preCtbname));
23!
240
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
20!
241

242
  pStmt->sql.autoCreateTbl = autoCreateTbl;
20✔
243

244
  return TSDB_CODE_SUCCESS;
20✔
245
}
246

247
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
248
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
249

250
  *pVgHash = pStmt->sql.pVgHash;
×
251
  pStmt->sql.pVgHash = NULL;
×
252

253
  *pBlockHash = pStmt->exec.pBlockHash;
×
254
  pStmt->exec.pBlockHash = NULL;
×
255

256
  return TSDB_CODE_SUCCESS;
×
257
}
258

259
static int32_t stmtParseSql(STscStmt2* pStmt) {
24✔
260
  pStmt->exec.pCurrBlock = NULL;
24✔
261

262
  SStmtCallback stmtCb = {
24✔
263
      .pStmt = pStmt,
264
      .getTbNameFn = stmtGetTbName,
265
      .setInfoFn = stmtUpdateInfo,
266
      .getExecInfoFn = stmtGetExecInfo,
267
  };
268

269
  STMT_ERR_RET(stmtCreateRequest(pStmt));
24!
270

271
  pStmt->stat.parseSqlNum++;
24✔
272
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
24!
273
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
24✔
274

275
  pStmt->bInfo.needParse = false;
24✔
276

277
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
24!
278
    pStmt->sql.type = STMT_TYPE_INSERT;
×
279
    pStmt->sql.stbInterlaceMode = false;
×
280
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
24!
281
    pStmt->sql.type = STMT_TYPE_QUERY;
×
282
    pStmt->sql.stbInterlaceMode = false;
×
283

284
    return TSDB_CODE_SUCCESS;
×
285
  }
286

287
  STableDataCxt** pSrc =
288
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
24✔
289
  if (NULL == pSrc || NULL == *pSrc) {
24!
290
    return terrno;
×
291
  }
292

293
  STableDataCxt* pTableCtx = *pSrc;
24✔
294
  // if (pStmt->sql.stbInterlaceMode) {
295
  //   int16_t lastIdx = -1;
296

297
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
298
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
299
  //       pStmt->sql.stbInterlaceMode = false;
300
  //       break;
301
  //     }
302

303
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
304
  //   }
305
  // }
306

307
  if (NULL == pStmt->sql.pBindInfo) {
24!
308
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
24!
309
    if (NULL == pStmt->sql.pBindInfo) {
24!
310
      return terrno;
×
311
    }
312
  }
313

314
  return TSDB_CODE_SUCCESS;
24✔
315
}
316

317
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
301✔
318
  pStmt->bInfo.tbUid = 0;
301✔
319
  pStmt->bInfo.tbVgId = -1;
301✔
320
  pStmt->bInfo.tbType = 0;
301✔
321
  pStmt->bInfo.needParse = true;
301✔
322
  pStmt->bInfo.inExecCache = false;
301✔
323

324
  pStmt->bInfo.tbName[0] = 0;
301✔
325
  pStmt->bInfo.tbFName[0] = 0;
301✔
326
  if (!pStmt->bInfo.tagsCached) {
301!
327
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
301✔
328
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
301!
329
  }
330
  if (!pStmt->sql.autoCreateTbl) {
300!
331
    pStmt->bInfo.stbFName[0] = 0;
301✔
332
    pStmt->bInfo.tbSuid = 0;
301✔
333
  }
334

335
  return TSDB_CODE_SUCCESS;
300✔
336
}
337

338
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
339
  (void)qResetStmtColumns(pTb->aCol, true);
×
340
  taosArrayDestroy(pTb->aCol);
×
341
}
×
342

343
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
253✔
344
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
253✔
345
  if (NULL == pTblBuf->pCurBuff) {
253!
346
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
347
    return;
×
348
  }
349
  pTblBuf->buffIdx = 1;
253✔
350
  pTblBuf->buffOffset = sizeof(*pQueue->head);
253✔
351

352
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
253✔
353
  pQueue->qRemainNum = 0;
253✔
354
  pQueue->head->next = NULL;
253✔
355
}
356

357
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
277✔
358
  if (pStmt->sql.stbInterlaceMode) {
277!
359
    if (deepClean) {
277✔
360
      taosHashCleanup(pStmt->exec.pBlockHash);
24✔
361
      pStmt->exec.pBlockHash = NULL;
24✔
362

363
      if (NULL != pStmt->exec.pCurrBlock) {
24!
364
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
24!
365
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
24✔
366
      }
367
    } else {
368
      pStmt->sql.siInfo.pTableColsIdx = 0;
253✔
369
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
253✔
370
    }
371
    if (NULL != pStmt->exec.pRequest) {
277!
372
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
277✔
373
    }
374
  } else {
375
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
×
376
      // if (!pStmt->options.asyncExecFn) {
377
      taos_free_result(pStmt->exec.pRequest);
×
378
      pStmt->exec.pRequest = NULL;
×
379
      //}
380
    }
381

382
    size_t keyLen = 0;
×
383
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
×
384
    while (pIter) {
×
385
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
×
386
      char*          key = taosHashGetKey(pIter, &keyLen);
×
387
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
×
388

389
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
×
390
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
×
391
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
×
392

393
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
×
394
        continue;
×
395
      }
396

397
      qDestroyStmtDataBlock(pBlocks);
×
398
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
×
399

400
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
×
401
    }
402

403
    if (keepTable) {
×
404
      return TSDB_CODE_SUCCESS;
×
405
    }
406

407
    taosHashCleanup(pStmt->exec.pBlockHash);
×
408
    pStmt->exec.pBlockHash = NULL;
×
409

410
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
×
411
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
×
412
  }
413

414
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
277!
415

416
  return TSDB_CODE_SUCCESS;
276✔
417
}
418

419
static void stmtFreeTbBuf(void* buf) {
24✔
420
  void* pBuf = *(void**)buf;
24✔
421
  taosMemoryFree(pBuf);
24!
422
}
24✔
423

424
static void stmtFreeTbCols(void* buf) {
24,000✔
425
  SArray* pCols = *(SArray**)buf;
24,000✔
426
  taosArrayDestroy(pCols);
24,000✔
427
}
24,000✔
428

429
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
24✔
430
  STMT_DLOG_E("start to free SQL info");
24!
431

432
  taosMemoryFreeClear(pStmt->db);
24!
433
  taosMemoryFree(pStmt->sql.pBindInfo);
24!
434
  taosMemoryFree(pStmt->sql.queryRes.fields);
24!
435
  taosMemoryFree(pStmt->sql.queryRes.userFields);
24!
436
  taosMemoryFree(pStmt->sql.sqlStr);
24!
437
  qDestroyQuery(pStmt->sql.pQuery);
24✔
438
  taosArrayDestroy(pStmt->sql.nodeList);
24✔
439
  taosHashCleanup(pStmt->sql.pVgHash);
24✔
440
  pStmt->sql.pVgHash = NULL;
24✔
441
  if (pStmt->sql.fixValueTags) {
24!
442
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
×
443
  }
444

445
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
24✔
446
  while (pIter) {
24!
447
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
448

449
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
450
    qDestroyBoundColInfo(pCache->boundTags);
×
451
    taosMemoryFreeClear(pCache->boundTags);
×
452

453
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
454
  }
455
  taosHashCleanup(pStmt->sql.pTableCache);
24✔
456
  pStmt->sql.pTableCache = NULL;
24✔
457

458
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
24!
459
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
24!
460

461
  taos_free_result(pStmt->sql.siInfo.pRequest);
24✔
462
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
24✔
463
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
24✔
464
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
24✔
465
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
24!
466
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
24✔
467
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
24✔
468

469
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
24✔
470
  pStmt->sql.siInfo.tableColsReady = true;
24✔
471

472
  STMT_DLOG_E("end to free SQL info");
24!
473

474
  return TSDB_CODE_SUCCESS;
24✔
475
}
476

477
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
×
478
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
×
479
    return TSDB_CODE_SUCCESS;
×
480
  }
481

482
  SVgroupInfo      vgInfo = {0};
×
483
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
484
                           .requestId = pStmt->exec.pRequest->requestId,
×
485
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
486
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
487

488
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
×
489
  if (TSDB_CODE_SUCCESS != code) {
×
490
    return code;
×
491
  }
492

493
  code =
494
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
×
495
  if (TSDB_CODE_SUCCESS != code) {
×
496
    return code;
×
497
  }
498

499
  *vgId = vgInfo.vgId;
×
500

501
  return TSDB_CODE_SUCCESS;
×
502
}
503

504
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
×
505
                                    uint64_t suid, int32_t vgId) {
506
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
507
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
×
508

509
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
×
510

511
  return TSDB_CODE_SUCCESS;
×
512
}
513

514
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
24✔
515
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
24!
516
    pStmt->bInfo.needParse = false;
×
517
    pStmt->bInfo.inExecCache = false;
×
518
    return TSDB_CODE_SUCCESS;
×
519
  }
520

521
  pStmt->bInfo.needParse = true;
24✔
522
  pStmt->bInfo.inExecCache = false;
24✔
523

524
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
24✔
525
  if (pCxtInExec) {
22!
526
    pStmt->bInfo.needParse = false;
×
527
    pStmt->bInfo.inExecCache = true;
×
528

529
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
530

531
    if (pStmt->sql.autoCreateTbl) {
×
532
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
533
      return TSDB_CODE_SUCCESS;
×
534
    }
535
  }
536

537
  if (NULL == pStmt->pCatalog) {
22!
538
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
22✔
539
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
23✔
540
  }
541

542
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
23!
543
    if (pStmt->bInfo.inExecCache) {
24!
544
      pStmt->bInfo.needParse = false;
×
545
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
546
      return TSDB_CODE_SUCCESS;
×
547
    }
548

549
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
24!
550
    return TSDB_CODE_SUCCESS;
24✔
551
  }
552

553
  if (pStmt->sql.autoCreateTbl) {
×
554
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
×
555
    if (pCache) {
×
556
      pStmt->bInfo.needParse = false;
×
557
      pStmt->bInfo.tbUid = 0;
×
558

559
      STableDataCxt* pNewBlock = NULL;
×
560
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
×
561

562
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
563
                      POINTER_BYTES)) {
564
        STMT_ERR_RET(terrno);
×
565
      }
566

567
      pStmt->exec.pCurrBlock = pNewBlock;
×
568

569
      tscDebug("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
×
570

571
      return TSDB_CODE_SUCCESS;
×
572
    }
573

574
    STMT_RET(stmtCleanBindInfo(pStmt));
×
575
  }
576

577
  uint64_t uid, suid;
578
  int32_t  vgId;
579
  int8_t   tableType;
580

581
  STableMeta*      pTableMeta = NULL;
×
582
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
583
                           .requestId = pStmt->exec.pRequest->requestId,
×
584
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
585
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
586
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
×
587

588
  pStmt->stat.ctgGetTbMetaNum++;
×
589

590
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
×
591
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
592
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
593

594
    STMT_ERR_RET(code);
×
595
  }
596

597
  STMT_ERR_RET(code);
×
598

599
  uid = pTableMeta->uid;
×
600
  suid = pTableMeta->suid;
×
601
  tableType = pTableMeta->tableType;
×
602
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
603
  vgId = pTableMeta->vgId;
×
604

605
  taosMemoryFree(pTableMeta);
×
606

607
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
608

609
  if (uid == pStmt->bInfo.tbUid) {
×
610
    pStmt->bInfo.needParse = false;
×
611

612
    tscDebug("tb %s is current table", pStmt->bInfo.tbFName);
×
613

614
    return TSDB_CODE_SUCCESS;
×
615
  }
616

617
  if (pStmt->bInfo.inExecCache) {
×
618
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
619
    if (NULL == pCache) {
×
620
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
621
               pStmt->bInfo.tbFName, uid, cacheUid);
622

623
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
624
    }
625

626
    pStmt->bInfo.needParse = false;
×
627

628
    pStmt->bInfo.tbUid = uid;
×
629
    pStmt->bInfo.tbSuid = suid;
×
630
    pStmt->bInfo.tbType = tableType;
×
631
    pStmt->bInfo.boundTags = pCache->boundTags;
×
632
    pStmt->bInfo.tagsCached = true;
×
633

634
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
×
635

636
    return TSDB_CODE_SUCCESS;
×
637
  }
638

639
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
640
  if (pCache) {
×
641
    pStmt->bInfo.needParse = false;
×
642

643
    pStmt->bInfo.tbUid = uid;
×
644
    pStmt->bInfo.tbSuid = suid;
×
645
    pStmt->bInfo.tbType = tableType;
×
646
    pStmt->bInfo.boundTags = pCache->boundTags;
×
647
    pStmt->bInfo.tagsCached = true;
×
648

649
    STableDataCxt* pNewBlock = NULL;
×
650
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
651

652
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
653
                    POINTER_BYTES)) {
654
      STMT_ERR_RET(terrno);
×
655
    }
656

657
    pStmt->exec.pCurrBlock = pNewBlock;
×
658

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

661
    return TSDB_CODE_SUCCESS;
×
662
  }
663

664
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
665

666
  return TSDB_CODE_SUCCESS;
×
667
}
668

669
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
670
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
671

672
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
673
  if (NULL == pStmt->sql.pTableCache) {
×
674
    STMT_ERR_RET(terrno);
×
675
  }
676

677
  pStmt->sql.status = STMT_INIT;
×
678

679
  return TSDB_CODE_SUCCESS;
×
680
}
681

682
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
806✔
683
  SStmtQNode* pParam = (SStmtQNode*)param;
806✔
684

685
  if (pParam->restoreTbCols) {
806✔
686
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
805✔
687
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
553✔
688
      *p = taosArrayInit(20, POINTER_BYTES);
553✔
689
      if (*p == NULL) {
553!
690
        STMT_ERR_RET(terrno);
×
691
      }
692
    }
693

694
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
252✔
695
  } else {
696
    int code = qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
553✔
697
                                      &pStmt->sql.siInfo, pParam->pCreateTbReq);
698
    // taosMemoryFree(pParam->pTbData);
699
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
553✔
700
    STMT_ERR_RET(code);
553!
701
  }
702
  return TSDB_CODE_SUCCESS;
806✔
703
}
704

705
static void* stmtBindThreadFunc(void* param) {
24✔
706
  setThreadName("stmtBind");
24✔
707

708
  qInfo("stmt bind thread started");
24!
709

710
  STscStmt2* pStmt = (STscStmt2*)param;
24✔
711

712
  while (true) {
830✔
713
    if (pStmt->queue.stopQueue) {
854✔
714
      break;
24✔
715
    }
716

717
    SStmtQNode* asyncParam = NULL;
830✔
718
    if (!stmtDequeue(pStmt, &asyncParam)) {
830✔
719
      continue;
24✔
720
    }
721

722
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
806!
723
      qError("stmt async output failed");
×
724
    }
725
  }
726

727
  qInfo("stmt bind thread stopped");
24!
728

729
  return NULL;
24✔
730
}
731

732
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
24✔
733
  TdThreadAttr thAttr;
734
  if (taosThreadAttrInit(&thAttr) != 0) {
24!
735
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
736
  }
737
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
24!
738
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
739
  }
740

741
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
24!
742
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
743
    STMT_ERR_RET(terrno);
×
744
  }
745

746
  pStmt->bindThreadInUse = true;
24✔
747

748
  (void)taosThreadAttrDestroy(&thAttr);
24✔
749
  return TSDB_CODE_SUCCESS;
24✔
750
}
751

752
static int32_t stmtInitQueue(STscStmt2* pStmt) {
24✔
753
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
24✔
754
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
24✔
755
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
48!
756
  pStmt->queue.tail = pStmt->queue.head;
24✔
757

758
  return TSDB_CODE_SUCCESS;
24✔
759
}
760

761
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
24✔
762
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
24✔
763
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
24✔
764
  pStmt->asyncBindParam.asyncBindNum = 0;
24✔
765

766
  return TSDB_CODE_SUCCESS;
24✔
767
}
768

769
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
24✔
770
  pTblBuf->buffUnit = sizeof(SStmtQNode);
24✔
771
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
24✔
772
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
24✔
773
  if (NULL == pTblBuf->pBufList) {
24!
774
    return terrno;
×
775
  }
776
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
24!
777
  if (NULL == buff) {
24!
778
    return terrno;
×
779
  }
780

781
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
48!
782
    return terrno;
×
783
  }
784

785
  pTblBuf->pCurBuff = buff;
24✔
786
  pTblBuf->buffIdx = 1;
24✔
787
  pTblBuf->buffOffset = 0;
24✔
788

789
  return TSDB_CODE_SUCCESS;
24✔
790
}
791

792
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
24✔
793
  STscObj*   pObj = (STscObj*)taos;
24✔
794
  STscStmt2* pStmt = NULL;
24✔
795
  int32_t    code = 0;
24✔
796

797
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
24!
798
  if (NULL == pStmt) {
24!
799
    return NULL;
×
800
  }
801

802
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
24✔
803
  if (NULL == pStmt->sql.pTableCache) {
24!
804
    taosMemoryFree(pStmt);
×
805
    return NULL;
×
806
  }
807

808
  pStmt->taos = pObj;
24✔
809
  pStmt->bInfo.needParse = true;
24✔
810
  pStmt->sql.status = STMT_INIT;
24✔
811
  pStmt->errCode = TSDB_CODE_SUCCESS;
24✔
812

813
  if (NULL != pOptions) {
24!
814
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
24✔
815
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
24!
816
      pStmt->stbInterlaceMode = true;
24✔
817
    }
818

819
    pStmt->reqid = pOptions->reqid;
24✔
820
  }
821

822
  if (pStmt->stbInterlaceMode) {
24!
823
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
24✔
824
    pStmt->sql.siInfo.acctId = taos->acctId;
24✔
825
    pStmt->sql.siInfo.dbname = taos->db;
24✔
826
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
24✔
827
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
24✔
828
    if (NULL == pStmt->sql.siInfo.pTableHash) {
24!
829
      (void)stmtClose2(pStmt);
×
830
      return NULL;
×
831
    }
832
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
24✔
833
    if (NULL == pStmt->sql.siInfo.pTableCols) {
24!
834
      terrno = terrno;
×
835
      (void)stmtClose2(pStmt);
×
836
      return NULL;
×
837
    }
838

839
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
24✔
840
    if (TSDB_CODE_SUCCESS == code) {
24!
841
      code = stmtInitQueue(pStmt);
24✔
842
    }
843
    if (TSDB_CODE_SUCCESS == code) {
24!
844
      code = stmtStartBindThread(pStmt);
24✔
845
    }
846
    if (TSDB_CODE_SUCCESS != code) {
24!
847
      terrno = code;
×
848
      (void)stmtClose2(pStmt);
×
849
      return NULL;
×
850
    }
851
  }
852

853
  pStmt->sql.siInfo.tableColsReady = true;
24✔
854
  if (pStmt->options.asyncExecFn) {
24!
855
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
×
856
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
857
      (void)stmtClose2(pStmt);
×
858
      return NULL;
×
859
    }
860
  }
861
  code = stmtIniAsyncBind(pStmt);
24✔
862
  if (TSDB_CODE_SUCCESS != code) {
24!
863
    terrno = code;
×
864
    (void)stmtClose2(pStmt);
×
865
    return NULL;
×
866
  }
867

868
  pStmt->execSemWaited = false;
24✔
869

870
  STMT_LOG_SEQ(STMT_INIT);
24!
871

872
  tscDebug("stmt:%p initialized", pStmt);
24!
873

874
  return pStmt;
24✔
875
}
876

877
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
878
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
879

880
  STMT_DLOG("start to set dbName:%s", dbName);
×
881

882
  pStmt->db = taosStrdup(dbName);
×
883
  (void)strdequote(pStmt->db);
×
884
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
885

886
  // The SQL statement specifies a database name, overriding the previously specified database
887
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
888
  pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
×
889
  if (pStmt->exec.pRequest->pDb == NULL) {
×
890
    return terrno;
×
891
  }
892
  if (pStmt->sql.stbInterlaceMode) {
×
893
    pStmt->sql.siInfo.dbname = pStmt->db;
×
894
  }
895
  return TSDB_CODE_SUCCESS;
×
896
}
897

898
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
24✔
899
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
900

901
  STMT_DLOG_E("start to prepare");
24!
902

903
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
24!
904
    return pStmt->errCode;
×
905
  }
906

907
  if (pStmt->sql.status >= STMT_PREPARE) {
24!
908
    STMT_ERR_RET(stmtResetStmt(pStmt));
×
909
  }
910

911
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
24!
912

913
  if (length <= 0) {
24!
914
    length = strlen(sql);
×
915
  }
916

917
  pStmt->sql.sqlStr = taosStrndup(sql, length);
24!
918
  if (!pStmt->sql.sqlStr) {
24!
919
    return terrno;
×
920
  }
921
  pStmt->sql.sqlLen = length;
24✔
922
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
24✔
923

924
  char* dbName = NULL;
24✔
925
  if (qParseDbName(sql, length, &dbName)) {
24!
926
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
927
    taosMemoryFreeClear(dbName);
×
928
  }
929

930
  return TSDB_CODE_SUCCESS;
24✔
931
}
932

933
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
24✔
934
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
24✔
935
  if (!pSrc) {
24!
936
    return terrno;
×
937
  }
938
  STableDataCxt* pDst = NULL;
24✔
939

940
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
24!
941
  pStmt->sql.siInfo.pDataCtx = pDst;
21✔
942

943
  SArray* pTblCols = NULL;
21✔
944
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
20,310✔
945
    pTblCols = taosArrayInit(20, POINTER_BYTES);
20,255✔
946
    if (NULL == pTblCols) {
21,847!
947
      return terrno;
×
948
    }
949

950
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
42,136!
951
      return terrno;
×
952
    }
953
  }
954

955
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
55✔
956

957
  return TSDB_CODE_SUCCESS;
55✔
958
}
959

960
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
1,106✔
961
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,106✔
962

963
  STMT_DLOG_E("start is insert");
1,106!
964

965
  if (pStmt->sql.type) {
1,106✔
966
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,082!
967
  } else {
968
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
24✔
969
  }
970

971
  return TSDB_CODE_SUCCESS;
1,106✔
972
}
973

974
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
553✔
975
  STscStmt2* pStmt = (STscStmt2*)stmt;
553✔
976

977
  int64_t startUs = taosGetTimestampUs();
552✔
978

979
  STMT_DLOG("start to set tbName:%s", tbName);
552!
980

981
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
553!
982
    return pStmt->errCode;
×
983
  }
984

985
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
553!
986

987
  int32_t insert = 0;
553✔
988
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
553!
989
  if (0 == insert) {
553!
990
    tscError("set tb name not available for none insert statement");
×
991
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
992
  }
993

994
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
553!
995
    STMT_ERR_RET(stmtCreateRequest(pStmt));
24!
996

997
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
24!
998
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
999
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
23!
1000

1001
    STMT_ERR_RET(stmtGetFromCache(pStmt));
24!
1002

1003
    if (pStmt->bInfo.needParse) {
24!
1004
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
24✔
1005
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
24✔
1006

1007
      STMT_ERR_RET(stmtParseSql(pStmt));
24!
1008
    }
1009
  } else {
1010
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
529✔
1011
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
529✔
1012
    pStmt->exec.pRequest->requestId++;
529✔
1013
    pStmt->bInfo.needParse = false;
529✔
1014
  }
1015

1016
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
553!
1017
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
24!
1018
  }
1019

1020
  int64_t startUs2 = taosGetTimestampUs();
553✔
1021
  pStmt->stat.setTbNameUs += startUs2 - startUs;
553✔
1022

1023
  return TSDB_CODE_SUCCESS;
553✔
1024
}
1025

1026
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
×
1027
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1028

1029
  STMT_DLOG_E("start to set tbTags");
×
1030

1031
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1032
    return pStmt->errCode;
×
1033
  }
1034

1035
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
×
1036

1037
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1038
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1039
    pStmt->bInfo.needParse = false;
×
1040
  }
1041
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1042

1043
  if (pStmt->bInfo.needParse) {
×
1044
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1045
  }
1046
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
1047
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1048
  }
1049

1050
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
×
1051
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1052
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1053
  //   return TSDB_CODE_SUCCESS;
1054
  // }
1055
  if (pStmt->sql.autoCreateTbl && pStmt->sql.stbInterlaceMode) {
×
1056
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
×
1057
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1058
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
×
1059
  }
1060

1061
  STableDataCxt** pDataBlock = NULL;
×
1062
  if (pStmt->exec.pCurrBlock) {
×
1063
    pDataBlock = &pStmt->exec.pCurrBlock;
×
1064
  } else {
1065
    pDataBlock =
1066
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1067
    if (NULL == pDataBlock) {
×
1068
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1069
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1070
    }
1071
    // pStmt->exec.pCurrBlock = *pDataBlock;
1072
    // if (pStmt->sql.stbInterlaceMode) {
1073
    //   taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
1074
    //   (*pDataBlock)->pData->aCol = NULL;
1075
    // }
1076
  }
1077
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
×
1078
    return TSDB_CODE_SUCCESS;
×
1079
  }
1080

1081
  tscDebug("start to bind stmt tag values");
×
1082

1083
  void* boundTags = NULL;
×
1084
  if (pStmt->sql.stbInterlaceMode) {
×
1085
    boundTags = pStmt->sql.siInfo.boundTags;
×
1086
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1087
    if (NULL == pCreateTbReq) {
×
1088
      return terrno;
×
1089
    }
1090
    int32_t vgId = -1;
×
1091
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
1092
    (*pCreateTbReq)->uid = vgId;
×
1093
  } else {
1094
    boundTags = pStmt->bInfo.boundTags;
×
1095
  }
1096

1097
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
×
1098
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1099
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1100

1101
  return TSDB_CODE_SUCCESS;
×
1102
}
1103

1104
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
×
1105
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1106

1107
  STMT_DLOG_E("start to set tbTags");
×
1108

1109
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1110
    return pStmt->errCode;
×
1111
  }
1112

1113
  if (!pStmt->sql.stbInterlaceMode) {
×
1114
    return TSDB_CODE_SUCCESS;
×
1115
  }
1116

1117
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
×
1118

1119
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1120
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1121
    pStmt->bInfo.needParse = false;
×
1122
  }
1123
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1124

1125
  if (pStmt->bInfo.needParse) {
×
1126
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1127
    if (!pStmt->sql.autoCreateTbl) {
×
1128
      return TSDB_CODE_SUCCESS;
×
1129
    }
1130
  }
1131

1132
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
1133
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1134
  }
1135

1136
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
×
1137
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1138
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
×
1139

1140
  STableDataCxt** pDataBlock = NULL;
×
1141
  if (pStmt->exec.pCurrBlock) {
×
1142
    pDataBlock = &pStmt->exec.pCurrBlock;
×
1143
  } else {
1144
    pDataBlock =
1145
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1146
    if (NULL == pDataBlock) {
×
1147
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1148
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1149
    }
1150
  }
1151

1152
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
×
1153
    return TSDB_CODE_SUCCESS;
×
1154
  }
1155

1156
  if (pStmt->sql.fixValueTags) {
×
1157
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
×
1158
    if ((*pCreateTbReq)->name) {
×
1159
      taosMemoryFree((*pCreateTbReq)->name);
×
1160
    }
1161
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
×
1162
    int32_t vgId = -1;
×
1163
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
1164
    (*pCreateTbReq)->uid = vgId;
×
1165
    return TSDB_CODE_SUCCESS;
×
1166
  }
1167

1168
  if ((*pDataBlock)->pData->pCreateTbReq) {
×
1169
    pStmt->sql.fixValueTags = true;
×
1170
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
×
1171
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
×
1172
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
×
1173
  }
1174

1175
  return TSDB_CODE_SUCCESS;
×
1176
}
1177

1178
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1179
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1180
    return pStmt->errCode;
×
1181
  }
1182

1183
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1184
    tscError("invalid operation to get query column fileds");
×
1185
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1186
  }
1187

1188
  STableDataCxt** pDataBlock = NULL;
×
1189

1190
  if (pStmt->sql.stbInterlaceMode) {
×
1191
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1192
  } else {
1193
    pDataBlock =
1194
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1195
    if (NULL == pDataBlock) {
×
1196
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1197
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1198
    }
1199
  }
1200

1201
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1202

1203
  return TSDB_CODE_SUCCESS;
×
1204
}
1205

1206
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
×
1207
  int32_t    code = 0;
×
1208
  int32_t    preCode = pStmt->errCode;
×
1209

1210
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1211
    return pStmt->errCode;
×
1212
  }
1213

1214
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1215
    tscError("invalid operation to get query column fileds");
×
1216
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1217
  }
1218

1219
  STableDataCxt** pDataBlock = NULL;
×
1220
  bool            cleanStb = false;
×
1221

1222
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
×
1223
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1224
  } else {
1225
    cleanStb = true;
×
1226
    pDataBlock =
1227
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1228
  }
1229

1230
  if (NULL == pDataBlock || NULL == *pDataBlock) {
×
1231
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1232
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1233
  }
1234

1235
  STMT_ERRI_JRET(
×
1236
      qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.preCtbname, fieldNum, fields));
1237
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
×
1238
    pStmt->bInfo.needParse = true;
×
1239
    qDestroyStmtDataBlock(*pDataBlock);
×
1240
    *pDataBlock = NULL;
×
1241
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
×
1242
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
1243
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1244
    }
1245
  }
1246

1247
_return:
×
1248

1249
  pStmt->errCode = preCode;
×
1250

1251
  return code;
×
1252
}
1253
/*
1254
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1255
  while (true) {
1256
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1257
      pStmt->exec.smInfo.pColIdx = 0;
1258
    }
1259

1260
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1261
      taosUsleep(1);
1262
      continue;
1263
    }
1264

1265
    *idx = pStmt->exec.smInfo.pColIdx;
1266
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1267
  }
1268
}
1269
*/
1270
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
551✔
1271
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
551✔
1272
    pStmt->sql.siInfo.pVgroupHash =
253✔
1273
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
252✔
1274
  }
1275
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
552✔
1276
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
253✔
1277
  }
1278

1279
  if (NULL == pStmt->sql.siInfo.pRequest) {
552✔
1280
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
24!
1281
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1282

1283
    if (pStmt->reqid != 0) {
24!
1284
      pStmt->reqid++;
×
1285
    }
1286
    pStmt->exec.pRequest->syncQuery = true;
24✔
1287

1288
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
24✔
1289
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
24✔
1290
  }
1291

1292
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
552✔
1293
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
36✔
1294
    pStmt->sql.siInfo.tbFromHash = true;
6✔
1295
  }
1296

1297
  if (0 == pStmt->sql.siInfo.firstName[0]) {
552✔
1298
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
24✔
1299
  }
1300

1301
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
552✔
1302
  param->next = NULL;
552✔
1303

1304
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
552✔
1305

1306
  stmtEnqueue(pStmt, param);
553✔
1307

1308
  return TSDB_CODE_SUCCESS;
553✔
1309
}
1310

1311
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1312
  while (true) {
1313
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
552!
1314
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
553✔
1315
      break;
553✔
1316
    } else {
1317
      SArray* pTblCols = NULL;
×
1318
      for (int32_t i = 0; i < 100; i++) {
×
1319
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1320
        if (NULL == pTblCols) {
×
1321
          return terrno;
×
1322
        }
1323

1324
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1325
          return terrno;
×
1326
        }
1327
      }
1328
    }
1329
  }
1330

1331
  return TSDB_CODE_SUCCESS;
553✔
1332
}
1333

1334
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
×
1335
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
×
1336
    return TSDB_CODE_SUCCESS;
×
1337
  }
1338

1339
  uint64_t uid = pStmt->bInfo.tbUid;
×
1340
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
×
1341

1342
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
×
1343
    return TSDB_CODE_SUCCESS;
×
1344
  }
1345

1346
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1347
  if (!pSrc) {
×
1348
    return terrno;
×
1349
  }
1350
  STableDataCxt* pDst = NULL;
×
1351

1352
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
×
1353

1354
  SStmtTableCache cache = {
×
1355
      .pDataCtx = pDst,
1356
      .boundTags = pStmt->bInfo.boundTags,
×
1357
  };
1358

1359
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
×
1360
    return terrno;
×
1361
  }
1362

1363
  if (pStmt->sql.autoCreateTbl) {
×
1364
    pStmt->bInfo.tagsCached = true;
×
1365
  } else {
1366
    pStmt->bInfo.boundTags = NULL;
×
1367
  }
1368

1369
  return TSDB_CODE_SUCCESS;
×
1370
}
1371

1372
static int stmtAddBatch2(TAOS_STMT2* stmt) {
253✔
1373
  STscStmt2* pStmt = (STscStmt2*)stmt;
253✔
1374

1375
  int64_t startUs = taosGetTimestampUs();
253✔
1376

1377
  STMT_DLOG_E("start to add batch");
253!
1378

1379
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
253!
1380
    return pStmt->errCode;
×
1381
  }
1382

1383
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
253!
1384

1385
  if (pStmt->sql.stbInterlaceMode) {
253!
1386
    int64_t startUs2 = taosGetTimestampUs();
253✔
1387
    pStmt->stat.addBatchUs += startUs2 - startUs;
253✔
1388

1389
    pStmt->sql.siInfo.tableColsReady = false;
253✔
1390

1391
    SStmtQNode* param = NULL;
253✔
1392
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
506!
1393
    param->restoreTbCols = true;
253✔
1394
    param->next = NULL;
253✔
1395

1396
    if (pStmt->sql.autoCreateTbl) {
253!
1397
      pStmt->bInfo.tagsCached = true;
×
1398
    }
1399

1400
    stmtEnqueue(pStmt, param);
253✔
1401

1402
    return TSDB_CODE_SUCCESS;
253✔
1403
  }
1404

1405
  STMT_ERR_RET(stmtCacheBlock(pStmt));
×
1406

1407
  return TSDB_CODE_SUCCESS;
×
1408
}
1409
/*
1410
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1411
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1412
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1413
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1414

1415
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1416
  pRes->fields = taosMemoryMalloc(size);
1417
  pRes->userFields = taosMemoryMalloc(size);
1418
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1419
    STMT_ERR_RET(terrno);
1420
  }
1421
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1422
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1423

1424
  return TSDB_CODE_SUCCESS;
1425
}
1426

1427
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1428
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1429
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1430

1431
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1432
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1433

1434
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1435
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1436
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1437
      STMT_ERR_RET(terrno);
1438
    }
1439
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1440
  }
1441

1442
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1443
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1444
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1445
      STMT_ERR_RET(terrno);
1446
    }
1447
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1448
  }
1449

1450
  return TSDB_CODE_SUCCESS;
1451
}
1452
*/
1453
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
550✔
1454
  STscStmt2* pStmt = (STscStmt2*)stmt;
550✔
1455
  int32_t    code = 0;
550✔
1456

1457
  int64_t startUs = taosGetTimestampUs();
551✔
1458

1459
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
551!
1460

1461
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
551!
1462
    return pStmt->errCode;
×
1463
  }
1464

1465
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
551!
1466

1467
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
551!
1468
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1469
    pStmt->bInfo.needParse = false;
×
1470
  }
1471

1472
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
551!
1473
    taos_free_result(pStmt->exec.pRequest);
×
1474
    pStmt->exec.pRequest = NULL;
×
1475
  }
1476

1477
  STMT_ERR_RET(stmtCreateRequest(pStmt));
551!
1478

1479
  if (pStmt->bInfo.needParse) {
551!
1480
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1481
  }
1482

1483
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
551!
1484
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
×
1485

1486
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
×
1487
                         .acctId = pStmt->taos->acctId,
×
1488
                         .db = pStmt->exec.pRequest->pDb,
×
1489
                         .topicQuery = false,
1490
                         .pSql = pStmt->sql.sqlStr,
×
1491
                         .sqlLen = pStmt->sql.sqlLen,
×
1492
                         .pMsg = pStmt->exec.pRequest->msgBuf,
×
1493
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1494
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
×
1495
                         .pStmtCb = NULL,
1496
                         .pUser = pStmt->taos->user};
×
1497
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
×
1498
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
×
1499

1500
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
×
1501

1502
    if (pStmt->sql.pQuery->haveResultSet) {
×
1503
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
×
1504
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1505
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
×
1506
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
×
1507
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
×
1508
    }
1509

1510
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
×
1511
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
×
1512
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
×
1513

1514
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1515
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1516
    // }
1517

1518
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1519

1520
    return TSDB_CODE_SUCCESS;
×
1521
  }
1522

1523
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
551!
1524
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1525
  }
1526

1527
  STableDataCxt** pDataBlock = NULL;
551✔
1528

1529
  if (pStmt->exec.pCurrBlock) {
551✔
1530
    pDataBlock = &pStmt->exec.pCurrBlock;
528✔
1531
  } else {
1532
    pDataBlock =
1533
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
23✔
1534
    if (NULL == pDataBlock) {
23!
1535
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1536
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1537
    }
1538
    pStmt->exec.pCurrBlock = *pDataBlock;
23✔
1539
    if (pStmt->sql.stbInterlaceMode) {
23!
1540
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
23✔
1541
      (*pDataBlock)->pData->aCol = NULL;
24✔
1542
    }
1543
    if (colIdx < -1) {
24!
1544
      pStmt->sql.bindRowFormat = true;
×
1545
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
1546
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
1547
    }
1548
  }
1549

1550
  int64_t startUs2 = taosGetTimestampUs();
552✔
1551
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
552✔
1552

1553
  SStmtQNode* param = NULL;
552✔
1554
  if (pStmt->sql.stbInterlaceMode) {
552!
1555
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,104!
1556
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
1,105!
1557
    taosArrayClear(param->tblData.aCol);
553✔
1558

1559
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1560

1561
    param->restoreTbCols = false;
553✔
1562
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
553✔
1563

1564
    param->pCreateTbReq = pCreateTbReq;
553✔
1565
  }
1566

1567
  int64_t startUs3 = taosGetTimestampUs();
553✔
1568
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
553✔
1569

1570
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
553!
1571

1572
  if (colIdx < 0) {
553!
1573
    if (pStmt->sql.stbInterlaceMode) {
553!
1574
      // (*pDataBlock)->pData->flags = 0;
1575
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
553✔
1576
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
553✔
1577
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
553✔
1578
                                    pStmt->taos->optionInfo.charsetCxt);
553✔
1579
    } else {
1580
      if (colIdx == -1) {
×
1581
        if (pStmt->sql.bindRowFormat) {
×
1582
          tscError("can't mix bind row format and bind column format");
×
1583
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1584
        }
1585
        code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1586
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
×
1587
      } else {
1588
        code = qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, bind, pStmt->exec.pRequest->msgBuf,
×
1589
                                  pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
×
1590
                                  pStmt->taos->optionInfo.charsetCxt);
×
1591
      }
1592
    }
1593

1594
    if (code) {
552!
1595
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
×
1596
      STMT_ERR_RET(code);
×
1597
    }
1598
  } else {
1599
    if (pStmt->sql.stbInterlaceMode) {
×
1600
      tscError("bind single column not allowed in stb insert mode");
×
1601
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1602
    }
1603

1604
    if (pStmt->sql.bindRowFormat) {
×
1605
      tscError("can't mix bind row format and bind column format");
×
1606
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1607
    }
1608

1609
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1610
      tscError("bind column index not in sequence");
×
1611
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1612
    }
1613

1614
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1615

1616
    if (0 == colIdx) {
×
1617
      pStmt->bInfo.sBindRowNum = bind->num;
×
1618
    }
1619

1620
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1621
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1622
    if (code) {
×
1623
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1624
      STMT_ERR_RET(code);
×
1625
    }
1626
  }
1627

1628
  int64_t startUs4 = taosGetTimestampUs();
552✔
1629
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
552✔
1630

1631
  if (pStmt->sql.stbInterlaceMode) {
552!
1632
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
552!
1633
  } else {
1634
    STMT_ERR_RET(stmtAddBatch2(pStmt));
×
1635
  }
1636

1637
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
553✔
1638

1639
  return TSDB_CODE_SUCCESS;
553✔
1640
}
1641
/*
1642
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1643
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1644

1645
  int32_t code = 0;
1646
  int32_t finalCode = 0;
1647
  size_t  keyLen = 0;
1648
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1649
  while (pIter) {
1650
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1651
    char*          key = taosHashGetKey(pIter, &keyLen);
1652

1653
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1654
    if (pMeta->uid) {
1655
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1656
      continue;
1657
    }
1658

1659
    SSubmitBlkRsp* blkRsp = NULL;
1660
    int32_t        i = 0;
1661
    for (; i < pRsp->nBlocks; ++i) {
1662
      blkRsp = pRsp->pBlocks + i;
1663
      if (strlen(blkRsp->tblFName) != keyLen) {
1664
        continue;
1665
      }
1666

1667
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1668
        continue;
1669
      }
1670

1671
      break;
1672
    }
1673

1674
    if (i < pRsp->nBlocks) {
1675
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1676
               blkRsp->uid);
1677

1678
      pMeta->uid = blkRsp->uid;
1679
      pStmt->bInfo.tbUid = blkRsp->uid;
1680
    } else {
1681
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1682
      if (NULL == pStmt->pCatalog) {
1683
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1684
        if (code) {
1685
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1686
          finalCode = code;
1687
          continue;
1688
        }
1689
      }
1690

1691
      code = stmtCreateRequest(pStmt);
1692
      if (code) {
1693
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1694
        finalCode = code;
1695
        continue;
1696
      }
1697

1698
      STableMeta*      pTableMeta = NULL;
1699
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1700
                               .requestId = pStmt->exec.pRequest->requestId,
1701
                               .requestObjRefId = pStmt->exec.pRequest->self,
1702
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1703
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1704

1705
      pStmt->stat.ctgGetTbMetaNum++;
1706

1707
      taos_free_result(pStmt->exec.pRequest);
1708
      pStmt->exec.pRequest = NULL;
1709

1710
      if (code || NULL == pTableMeta) {
1711
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1712
        finalCode = code;
1713
        taosMemoryFree(pTableMeta);
1714
        continue;
1715
      }
1716

1717
      pMeta->uid = pTableMeta->uid;
1718
      pStmt->bInfo.tbUid = pTableMeta->uid;
1719
      taosMemoryFree(pTableMeta);
1720
    }
1721

1722
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1723
  }
1724

1725
  return finalCode;
1726
}
1727
*/
1728
/*
1729
int stmtStaticModeExec(TAOS_STMT* stmt) {
1730
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1731
  int32_t     code = 0;
1732
  SSubmitRsp* pRsp = NULL;
1733
  if (pStmt->sql.staticMode) {
1734
    return TSDB_CODE_TSC_STMT_API_ERROR;
1735
  }
1736

1737
  STMT_DLOG_E("start to exec");
1738

1739
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1740

1741
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1742
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1743

1744
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1745

1746
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1747
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1748
    if (code) {
1749
      pStmt->exec.pRequest->code = code;
1750
    } else {
1751
      tFreeSSubmitRsp(pRsp);
1752
      STMT_ERR_RET(stmtResetStmt(pStmt));
1753
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1754
    }
1755
  }
1756

1757
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1758

1759
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1760
  pStmt->affectedRows += pStmt->exec.affectedRows;
1761

1762
_return:
1763

1764
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1765

1766
  tFreeSSubmitRsp(pRsp);
1767

1768
  ++pStmt->sql.runTimes;
1769

1770
  STMT_RET(code);
1771
}
1772
*/
1773

1774
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
1775
  const STscObj* pTscObj = pRequest->pTscObj;
×
1776

1777
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
1778
  if (*pCxt == NULL) {
×
1779
    return terrno;
×
1780
  }
1781

1782
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
1783
                           .requestRid = pRequest->self,
×
1784
                           .acctId = pTscObj->acctId,
×
1785
                           .db = pRequest->pDb,
×
1786
                           .topicQuery = false,
1787
                           .pSql = pRequest->sqlstr,
×
1788
                           .sqlLen = pRequest->sqlLen,
×
1789
                           .pMsg = pRequest->msgBuf,
×
1790
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1791
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
1792
                           .pStmtCb = NULL,
1793
                           .pUser = pTscObj->user,
×
1794
                           .pEffectiveUser = pRequest->effectiveUser,
×
1795
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
1796
                           .enableSysInfo = pTscObj->sysInfo,
×
1797
                           .async = true,
1798
                           .svrVer = pTscObj->sVer,
×
1799
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
1800
                           .allocatorId = pRequest->allocatorRefId,
×
1801
                           .parseSqlFp = clientParseSql,
1802
                           .parseSqlParam = pWrapper};
1803
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
1804
  (*pCxt)->biMode = biMode;
×
1805
  return TSDB_CODE_SUCCESS;
×
1806
}
1807

1808
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
1809
  STscStmt2*        pStmt = userdata;
×
1810
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1811

1812
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1813
  pStmt->affectedRows += pStmt->exec.affectedRows;
×
1814

1815
  fp(pStmt->options.userdata, res, code);
×
1816

1817
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1818
    taosUsleep(1);
×
1819
  }
1820
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
1821
  ++pStmt->sql.runTimes;
×
1822

1823
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
1824
    tscError("failed to post asyncExecSem");
×
1825
  }
1826
}
×
1827

1828
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
253✔
1829
  STscStmt2* pStmt = (STscStmt2*)stmt;
253✔
1830
  int32_t    code = 0;
253✔
1831
  int64_t    startUs = taosGetTimestampUs();
253✔
1832

1833
  STMT_DLOG_E("start to exec");
253!
1834

1835
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
253!
1836
    return pStmt->errCode;
×
1837
  }
1838

1839
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
253!
1840
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
253!
1841
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1842
  }
1843
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
253!
1844

1845
  if (pStmt->sql.stbInterlaceMode) {
253!
1846
    STMT_ERR_RET(stmtAddBatch2(pStmt));
253!
1847
  }
1848

1849
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
253!
1850

1851
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
253!
1852
    if (pStmt->sql.stbInterlaceMode) {
253!
1853
      int64_t startTs = taosGetTimestampUs();
253✔
1854
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
521✔
1855
        taosUsleep(1);
268✔
1856
      }
1857
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
253✔
1858

1859
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
253!
1860
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
253✔
1861
      pStmt->sql.siInfo.pVgroupHash = NULL;
253✔
1862
      pStmt->sql.siInfo.pVgroupList = NULL;
253✔
1863
    } else {
1864
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
×
1865
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
×
1866

1867
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
×
1868

1869
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
×
1870
    }
1871
  }
1872

1873
  SRequestObj*      pRequest = pStmt->exec.pRequest;
253✔
1874
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
253✔
1875

1876
  if (!fp) {
253!
1877
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
253✔
1878

1879
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
253!
1880
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1881
      if (code) {
×
1882
        pStmt->exec.pRequest->code = code;
×
1883
      } else {
1884
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1885
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1886
      }
1887
    }
1888

1889
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
253!
1890

1891
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
253✔
1892
    if (affected_rows) {
253!
1893
      *affected_rows = pStmt->exec.affectedRows;
253✔
1894
    }
1895
    pStmt->affectedRows += pStmt->exec.affectedRows;
253✔
1896

1897
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
253!
1898
      taosUsleep(1);
×
1899
    }
1900

1901
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
252!
1902

1903
    ++pStmt->sql.runTimes;
252✔
1904
  } else {
1905
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
1906
    if (pWrapper == NULL) {
×
1907
      code = terrno;
×
1908
    } else {
1909
      pWrapper->pRequest = pRequest;
×
1910
      pRequest->pWrapper = pWrapper;
×
1911
    }
1912
    if (TSDB_CODE_SUCCESS == code) {
×
1913
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
1914
    }
1915
    pRequest->syncQuery = false;
×
1916
    pRequest->body.queryFp = asyncQueryCb;
×
1917
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
1918

1919
    pStmt->execSemWaited = false;
×
1920
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
1921
  }
1922

1923
_return:
252✔
1924
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
253✔
1925

1926
  STMT_RET(code);
253!
1927
}
1928

1929
int stmtClose2(TAOS_STMT2* stmt) {
24✔
1930
  STscStmt2* pStmt = (STscStmt2*)stmt;
24✔
1931

1932
  STMT_DLOG_E("start to free stmt");
24!
1933

1934
  if (pStmt->bindThreadInUse) {
24!
1935
    pStmt->queue.stopQueue = true;
24✔
1936

1937
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
24✔
1938
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
24✔
1939
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
24✔
1940
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
24✔
1941

1942
    (void)taosThreadJoin(pStmt->bindThread, NULL);
24✔
1943
    pStmt->bindThreadInUse = false;
24✔
1944

1945
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
24✔
1946
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
24✔
1947
  }
1948

1949
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
24!
1950
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
24!
1951
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1952
  }
1953
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
24!
1954

1955
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
24✔
1956
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
24✔
1957

1958
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
24!
1959
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1960
      tscError("failed to wait asyncExecSem");
×
1961
    }
1962
  }
1963

1964
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
24!
1965
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1966
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1967
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1968
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1969
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1970
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1971
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1972
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1973
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1974

1975
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
24!
1976

1977
  if (pStmt->options.asyncExecFn) {
24!
1978
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
1979
      tscError("failed to destroy asyncExecSem");
×
1980
    }
1981
  }
1982
  taosMemoryFree(stmt);
24!
1983

1984
  return TSDB_CODE_SUCCESS;
24✔
1985
}
1986

1987
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
1988
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1989

1990
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
1991
    return (char*)tstrerror(terrno);
×
1992
  }
1993

1994
  pStmt->exec.pRequest->code = terrno;
×
1995

1996
  return taos_errstr(pStmt->exec.pRequest);
×
1997
}
1998
/*
1999
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2000

2001
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2002
*/
2003

2004
int stmtParseColFields2(TAOS_STMT2* stmt) {
×
2005
  int32_t    code = 0;
×
2006
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2007
  int32_t    preCode = pStmt->errCode;
×
2008

2009
  STMT_DLOG_E("start to get col fields");
×
2010

2011
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2012
    return pStmt->errCode;
×
2013
  }
2014

2015
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2016
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2017
  }
2018

2019
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2020

2021
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2022
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2023
    pStmt->bInfo.needParse = false;
×
2024
  }
2025

2026
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2027
    taos_free_result(pStmt->exec.pRequest);
×
2028
    pStmt->exec.pRequest = NULL;
×
2029
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2030
  }
2031

2032
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2033

2034
  if (pStmt->bInfo.needParse) {
×
2035
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2036
  }
2037

2038
_return:
×
2039

2040
  pStmt->errCode = preCode;
×
2041

2042
  return code;
×
2043
}
2044

2045
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
×
2046
  int32_t code = stmtParseColFields2(stmt);
×
2047
  if (code != TSDB_CODE_SUCCESS) {
×
2048
    return code;
×
2049
  }
2050

2051
  return stmtFetchStbColFields2(stmt, nums, fields);
×
2052
}
2053

2054
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2055
  int32_t    code = 0;
×
2056
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2057
  int32_t    preCode = pStmt->errCode;
×
2058

2059
  STMT_DLOG_E("start to get param num");
×
2060

2061
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2062
    return pStmt->errCode;
×
2063
  }
2064

2065
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2066

2067
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2068
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2069
    pStmt->bInfo.needParse = false;
×
2070
  }
2071

2072
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2073
    taos_free_result(pStmt->exec.pRequest);
×
2074
    pStmt->exec.pRequest = NULL;
×
2075
  }
2076

2077
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2078

2079
  if (pStmt->bInfo.needParse) {
×
2080
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2081
  }
2082

2083
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2084
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2085
  } else {
2086
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2087
  }
2088

2089
_return:
×
2090

2091
  pStmt->errCode = preCode;
×
2092

2093
  return code;
×
2094
}
2095

2096
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
×
2097
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2098

2099
  STMT_DLOG_E("start to use result");
×
2100

2101
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
2102
    tscError("useResult only for query statement");
×
2103
    return NULL;
×
2104
  }
2105

2106
  return pStmt->exec.pRequest;
×
2107
}
2108

2109
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2110
  qInfo("async stmt bind thread started");
×
2111

2112
  ThreadArgs* targs = (ThreadArgs*)args;
×
2113
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2114

2115
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2116
  targs->fp(targs->param, NULL, code);
×
2117
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2118
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2119
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2120
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2121
  taosMemoryFree(args);
×
2122

2123
  qInfo("async stmt bind thread stopped");
×
2124

2125
  return code;
×
2126
}
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