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

taosdata / TDengine / #3559

18 Dec 2024 12:59AM UTC coverage: 59.805% (+0.03%) from 59.778%
#3559

push

travis-ci

web-flow
Merge pull request #29187 from taosdata/merge/mainto3.0

merge: main to 3.0 branch

132705 of 287544 branches covered (46.15%)

Branch coverage included in aggregate %.

87 of 95 new or added lines in 19 files covered. (91.58%)

1132 existing lines in 133 files now uncovered.

209591 of 284807 relevant lines covered (73.59%)

8125235.78 hits per line

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

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

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
×
42
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
×
43
    taosUsleep(1);
×
44
    return false;
×
45
  }
46

47
  SStmtQNode* orig = pStmt->queue.head;
×
48

49
  SStmtQNode* node = pStmt->queue.head->next;
×
50
  pStmt->queue.head = pStmt->queue.head->next;
×
51

52
  // taosMemoryFreeClear(orig);
53

54
  *param = node;
×
55

56
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
×
57

58
  return true;
×
59
}
60

61
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
×
62
  pStmt->queue.tail->next = param;
×
63
  pStmt->queue.tail = param;
×
64

65
  pStmt->stat.bindDataNum++;
×
66
  (void)atomic_add_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
×
67
}
×
68

69
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
×
70
  int32_t code = 0;
×
71

72
  if (pStmt->exec.pRequest == NULL) {
×
73
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
×
74
                        pStmt->reqid);
75
    if (pStmt->reqid != 0) {
×
76
      pStmt->reqid++;
×
77
    }
78
    if (pStmt->db != NULL) {
×
79
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb); 
×
NEW
80
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
×
81
    }
82
    if (TSDB_CODE_SUCCESS == code) {
×
83
      pStmt->exec.pRequest->syncQuery = true;
×
84
      pStmt->exec.pRequest->isStmtBind = true;
×
85
    }
86
  }
87

88
  return code;
×
89
}
90

91
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
×
92
  int32_t code = 0;
×
93

94
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
×
95
    STMT_LOG_SEQ(newStatus);
×
96
  }
97

98
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
×
99
    STMT_DLOG("stmt already failed with err: %s", tstrerror(pStmt->errCode));
×
100
    return pStmt->errCode;
×
101
  }
102

103
  switch (newStatus) {
×
104
    case STMT_PREPARE:
×
105
      pStmt->errCode = 0;
×
106
      break;
×
107
    case STMT_SETTBNAME:
×
108
      if (STMT_STATUS_EQ(INIT)) {
×
109
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
110
      }
111
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
×
112
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
113
      }
114
      break;
×
115
    case STMT_SETTAGS:
×
116
      if (STMT_STATUS_EQ(INIT)) {
×
117
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
118
      }
119
      break;
×
120
    case STMT_FETCH_FIELDS:
×
121
      if (STMT_STATUS_EQ(INIT)) {
×
122
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
123
      }
124
      break;
×
125
    case STMT_BIND:
×
126
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
×
127
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
128
      }
129
      /*
130
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
131
              code = TSDB_CODE_TSC_STMT_API_ERROR;
132
            }
133
      */
134
      break;
×
135
    case STMT_BIND_COL:
×
136
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
137
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
138
      }
139
      break;
×
140
    case STMT_ADD_BATCH:
×
141
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
×
142
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
143
      }
144
      break;
×
145
    case STMT_EXECUTE:
×
146
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
147
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
×
148
            STMT_STATUS_NE(BIND_COL)) {
×
149
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
        }
151
      } else {
152
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
×
153
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
154
        }
155
      }
156
      break;
×
157
    default:
×
158
      code = TSDB_CODE_APP_ERROR;
×
159
      break;
×
160
  }
161

162
  STMT_ERR_RET(code);
×
163

164
  pStmt->sql.status = newStatus;
×
165

166
  return TSDB_CODE_SUCCESS;
×
167
}
168

169
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
×
170
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
171

172
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
×
173

174
  if ('\0' == pStmt->bInfo.tbName[0]) {
×
175
    tscError("no table name set");
×
176
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
×
177
  }
178

179
  *tbName = pStmt->bInfo.tbName;
×
180

181
  return TSDB_CODE_SUCCESS;
×
182
}
183

184
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName,
×
185
                                  const char* sTableName, bool autoCreateTbl, bool preCtbname) {
186
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
187
  char       tbFName[TSDB_TABLE_FNAME_LEN];
188
  int32_t    code = tNameExtractFullName(tbName, tbFName);
×
189
  if (code != 0) {
×
190
    return code;
×
191
  }
192

193
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
×
194
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
×
195
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
×
196

197
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
×
198
  pStmt->bInfo.tbSuid = pTableMeta->suid;
×
199
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
200
  pStmt->bInfo.tbType = pTableMeta->tableType;
×
201

202
  if (!pStmt->bInfo.tagsCached) {
×
203
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
×
204
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
×
205
  }
206

207
  pStmt->bInfo.boundTags = tags;
×
208
  pStmt->bInfo.tagsCached = false;
×
209
  pStmt->bInfo.preCtbname = preCtbname;
×
210
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
×
211

212
  return TSDB_CODE_SUCCESS;
×
213
}
214

215
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
×
216
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
217

218
  pStmt->sql.pVgHash = pVgHash;
×
219
  pStmt->exec.pBlockHash = pBlockHash;
×
220

221
  return TSDB_CODE_SUCCESS;
×
222
}
223

224
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
×
225
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
226
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
227

228
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, preCtbname));
×
229
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
×
230

231
  pStmt->sql.autoCreateTbl = autoCreateTbl;
×
232
  if (pStmt->sql.autoCreateTbl) {
×
233
    pStmt->sql.stbInterlaceMode = false;
×
234
  }
235

236
  return TSDB_CODE_SUCCESS;
×
237
}
238

239
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
240
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
241

242
  *pVgHash = pStmt->sql.pVgHash;
×
243
  pStmt->sql.pVgHash = NULL;
×
244

245
  *pBlockHash = pStmt->exec.pBlockHash;
×
246
  pStmt->exec.pBlockHash = NULL;
×
247

248
  return TSDB_CODE_SUCCESS;
×
249
}
250

251
static int32_t stmtParseSql(STscStmt2* pStmt) {
×
252
  pStmt->exec.pCurrBlock = NULL;
×
253

254
  SStmtCallback stmtCb = {
×
255
      .pStmt = pStmt,
256
      .getTbNameFn = stmtGetTbName,
257
      .setInfoFn = stmtUpdateInfo,
258
      .getExecInfoFn = stmtGetExecInfo,
259
  };
260

261
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
262

263
  pStmt->stat.parseSqlNum++;
×
264
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
×
265
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
×
266

267
  pStmt->bInfo.needParse = false;
×
268

269
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
×
270
    pStmt->sql.type = STMT_TYPE_INSERT;
×
271
    pStmt->sql.stbInterlaceMode = false;
×
272
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
×
273
    pStmt->sql.type = STMT_TYPE_QUERY;
×
274
    pStmt->sql.stbInterlaceMode = false;
×
275

276
    return TSDB_CODE_SUCCESS;
×
277
  }
278

279
  STableDataCxt** pSrc =
280
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
281
  if (NULL == pSrc || NULL == *pSrc) {
×
282
    return terrno;
×
283
  }
284

285
  STableDataCxt* pTableCtx = *pSrc;
×
286
  if (pStmt->sql.stbInterlaceMode) {
×
287
    int16_t lastIdx = -1;
×
288

289
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
×
290
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
×
291
        pStmt->sql.stbInterlaceMode = false;
×
292
        break;
×
293
      }
294

295
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
×
296
    }
297
  }
298

299
  if (NULL == pStmt->sql.pBindInfo) {
×
300
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
×
301
    if (NULL == pStmt->sql.pBindInfo) {
×
302
      return terrno;
×
303
    }
304
  }
305

306
  return TSDB_CODE_SUCCESS;
×
307
}
308

309
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
×
310
  pStmt->bInfo.tbUid = 0;
×
311
  pStmt->bInfo.tbSuid = 0;
×
312
  pStmt->bInfo.tbVgId = -1;
×
313
  pStmt->bInfo.tbType = 0;
×
314
  pStmt->bInfo.needParse = true;
×
315
  pStmt->bInfo.inExecCache = false;
×
316

317
  pStmt->bInfo.tbName[0] = 0;
×
318
  pStmt->bInfo.tbFName[0] = 0;
×
319
  if (!pStmt->bInfo.tagsCached) {
×
320
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
×
321
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
×
322
  }
323
  pStmt->bInfo.stbFName[0] = 0;
×
324

325
  return TSDB_CODE_SUCCESS;
×
326
}
327

328
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
329
  (void)qResetStmtColumns(pTb->aCol, true);
×
330
  taosArrayDestroy(pTb->aCol);
×
331
}
×
332

333
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
×
334
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
×
335
  if (NULL == pTblBuf->pCurBuff) {
×
336
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
337
    return;
×
338
  }
339
  pTblBuf->buffIdx = 1;
×
340
  pTblBuf->buffOffset = sizeof(*pQueue->head);
×
341

342
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
×
343
  pQueue->qRemainNum = 0;
×
344
  pQueue->head->next = NULL;
×
345
}
346

347
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
×
348
  if (pStmt->sql.stbInterlaceMode) {
×
349
    if (deepClean) {
×
350
      taosHashCleanup(pStmt->exec.pBlockHash);
×
351
      pStmt->exec.pBlockHash = NULL;
×
352

353
      if (NULL != pStmt->exec.pCurrBlock) {
×
354
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
×
355
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
×
356
      }
357
    } else {
358
      pStmt->sql.siInfo.pTableColsIdx = 0;
×
359
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
×
360
    }
361
  } else {
362
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
×
363
      // if (!pStmt->options.asyncExecFn) {
364
      taos_free_result(pStmt->exec.pRequest);
×
365
      pStmt->exec.pRequest = NULL;
×
366
      //}
367
    }
368

369
    size_t keyLen = 0;
×
370
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
×
371
    while (pIter) {
×
372
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
×
373
      char*          key = taosHashGetKey(pIter, &keyLen);
×
374
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
×
375

376
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
×
377
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
×
378
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
×
379

380
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
×
381
        continue;
×
382
      }
383

384
      qDestroyStmtDataBlock(pBlocks);
×
385
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
×
386

387
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
×
388
    }
389

390
    if (keepTable) {
×
391
      return TSDB_CODE_SUCCESS;
×
392
    }
393

394
    taosHashCleanup(pStmt->exec.pBlockHash);
×
395
    pStmt->exec.pBlockHash = NULL;
×
396

397
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
×
398
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
×
399
  }
400

401
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
402

403
  return TSDB_CODE_SUCCESS;
×
404
}
405

406
static void stmtFreeTbBuf(void* buf) {
×
407
  void* pBuf = *(void**)buf;
×
408
  taosMemoryFree(pBuf);
×
409
}
×
410

411
static void stmtFreeTbCols(void* buf) {
×
412
  SArray* pCols = *(SArray**)buf;
×
413
  taosArrayDestroy(pCols);
×
414
}
×
415

416
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
×
417
  STMT_DLOG_E("start to free SQL info");
×
418

419
  taosMemoryFreeClear(pStmt->db);
×
420
  taosMemoryFree(pStmt->sql.pBindInfo);
×
421
  taosMemoryFree(pStmt->sql.queryRes.fields);
×
422
  taosMemoryFree(pStmt->sql.queryRes.userFields);
×
423
  taosMemoryFree(pStmt->sql.sqlStr);
×
424
  qDestroyQuery(pStmt->sql.pQuery);
×
425
  taosArrayDestroy(pStmt->sql.nodeList);
×
426
  taosHashCleanup(pStmt->sql.pVgHash);
×
427
  pStmt->sql.pVgHash = NULL;
×
428

429
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
×
430
  while (pIter) {
×
431
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
432

433
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
434
    qDestroyBoundColInfo(pCache->boundTags);
×
435
    taosMemoryFreeClear(pCache->boundTags);
×
436

437
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
438
  }
439
  taosHashCleanup(pStmt->sql.pTableCache);
×
440
  pStmt->sql.pTableCache = NULL;
×
441

442
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
×
443
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
444

445
  taos_free_result(pStmt->sql.siInfo.pRequest);
×
446
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
447
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
×
448
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
×
449
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
×
450
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
×
451
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
×
452

453
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
×
454
  pStmt->sql.siInfo.tableColsReady = true;
×
455

456
  STMT_DLOG_E("end to free SQL info");
×
457

458
  return TSDB_CODE_SUCCESS;
×
459
}
460

461
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
×
462
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
×
463
    return TSDB_CODE_SUCCESS;
×
464
  }
465

466
  SVgroupInfo      vgInfo = {0};
×
467
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
468
                           .requestId = pStmt->exec.pRequest->requestId,
×
469
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
470
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
471

472
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
×
473
  if (TSDB_CODE_SUCCESS != code) {
×
474
    return code;
×
475
  }
476

477
  code =
478
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
×
479
  if (TSDB_CODE_SUCCESS != code) {
×
480
    return code;
×
481
  }
482

483
  *vgId = vgInfo.vgId;
×
484

485
  return TSDB_CODE_SUCCESS;
×
486
}
487

488
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
×
489
                                    uint64_t suid, int32_t vgId) {
490
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
491
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
×
492

493
  STMT_DLOG("tableDataCxt rebuilt, uid:%" PRId64 ", vgId:%d", uid, vgId);
×
494

495
  return TSDB_CODE_SUCCESS;
×
496
}
497

498
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
×
499
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
×
500
    pStmt->bInfo.needParse = false;
×
501
    pStmt->bInfo.inExecCache = false;
×
502
    return TSDB_CODE_SUCCESS;
×
503
  }
504

505
  pStmt->bInfo.needParse = true;
×
506
  pStmt->bInfo.inExecCache = false;
×
507

508
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
509
  if (pCxtInExec) {
×
510
    pStmt->bInfo.needParse = false;
×
511
    pStmt->bInfo.inExecCache = true;
×
512

513
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
514

515
    if (pStmt->sql.autoCreateTbl) {
×
516
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
517
      return TSDB_CODE_SUCCESS;
×
518
    }
519
  }
520

521
  if (NULL == pStmt->pCatalog) {
×
522
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
×
523
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
×
524
  }
525

526
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
×
527
    if (pStmt->bInfo.inExecCache) {
×
528
      pStmt->bInfo.needParse = false;
×
529
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
530
      return TSDB_CODE_SUCCESS;
×
531
    }
532

533
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
×
534
    return TSDB_CODE_SUCCESS;
×
535
  }
536

537
  if (pStmt->sql.autoCreateTbl) {
×
538
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
×
539
    if (pCache) {
×
540
      pStmt->bInfo.needParse = false;
×
541
      pStmt->bInfo.tbUid = 0;
×
542

543
      STableDataCxt* pNewBlock = NULL;
×
544
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
×
545

546
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
547
                      POINTER_BYTES)) {
548
        STMT_ERR_RET(terrno);
×
549
      }
550

551
      pStmt->exec.pCurrBlock = pNewBlock;
×
552

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

555
      return TSDB_CODE_SUCCESS;
×
556
    }
557

558
    STMT_RET(stmtCleanBindInfo(pStmt));
×
559
  }
560

561
  uint64_t uid, suid;
562
  int32_t  vgId;
563
  int8_t   tableType;
564

565
  STableMeta*      pTableMeta = NULL;
×
566
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
×
567
                           .requestId = pStmt->exec.pRequest->requestId,
×
568
                           .requestObjRefId = pStmt->exec.pRequest->self,
×
569
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
×
570
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
×
571

572
  pStmt->stat.ctgGetTbMetaNum++;
×
573

574
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
×
575
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
576
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
577

578
    STMT_ERR_RET(code);
×
579
  }
580

581
  STMT_ERR_RET(code);
×
582

583
  uid = pTableMeta->uid;
×
584
  suid = pTableMeta->suid;
×
585
  tableType = pTableMeta->tableType;
×
586
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
587
  vgId = pTableMeta->vgId;
×
588

589
  taosMemoryFree(pTableMeta);
×
590

591
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
592

593
  if (uid == pStmt->bInfo.tbUid) {
×
594
    pStmt->bInfo.needParse = false;
×
595

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

598
    return TSDB_CODE_SUCCESS;
×
599
  }
600

601
  if (pStmt->bInfo.inExecCache) {
×
602
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
603
    if (NULL == pCache) {
×
604
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
605
               pStmt->bInfo.tbFName, uid, cacheUid);
606

607
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
608
    }
609

610
    pStmt->bInfo.needParse = false;
×
611

612
    pStmt->bInfo.tbUid = uid;
×
613
    pStmt->bInfo.tbSuid = suid;
×
614
    pStmt->bInfo.tbType = tableType;
×
615
    pStmt->bInfo.boundTags = pCache->boundTags;
×
616
    pStmt->bInfo.tagsCached = true;
×
617

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

620
    return TSDB_CODE_SUCCESS;
×
621
  }
622

623
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
624
  if (pCache) {
×
625
    pStmt->bInfo.needParse = false;
×
626

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

633
    STableDataCxt* pNewBlock = NULL;
×
634
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
635

636
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
637
                    POINTER_BYTES)) {
638
      STMT_ERR_RET(terrno);
×
639
    }
640

641
    pStmt->exec.pCurrBlock = pNewBlock;
×
642

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

645
    return TSDB_CODE_SUCCESS;
×
646
  }
647

648
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
649

650
  return TSDB_CODE_SUCCESS;
×
651
}
652

653
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
654
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
655

656
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
657
  if (NULL == pStmt->sql.pTableCache) {
×
658
    STMT_ERR_RET(terrno);
×
659
  }
660

661
  pStmt->sql.status = STMT_INIT;
×
662

663
  return TSDB_CODE_SUCCESS;
×
664
}
665

666
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
×
667
  SStmtQNode* pParam = (SStmtQNode*)param;
×
668

669
  if (pParam->restoreTbCols) {
×
670
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
×
671
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
×
672
      *p = taosArrayInit(20, POINTER_BYTES);
×
673
      if (*p == NULL) {
×
674
        STMT_ERR_RET(terrno);
×
675
      }
676
    }
677

678
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
×
679
  } else {
680
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
×
681
                                        &pStmt->sql.siInfo));
682

683
    // taosMemoryFree(pParam->pTbData);
684

685
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
×
686
  }
687
  return TSDB_CODE_SUCCESS;
×
688
}
689

690
static void* stmtBindThreadFunc(void* param) {
×
691
  setThreadName("stmtBind");
×
692

693
  qInfo("stmt bind thread started");
×
694

695
  STscStmt2* pStmt = (STscStmt2*)param;
×
696

697
  while (true) {
×
698
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
×
699
      break;
×
700
    }
701

702
    SStmtQNode* asyncParam = NULL;
×
703
    if (!stmtDequeue(pStmt, &asyncParam)) {
×
704
      continue;
×
705
    }
706

707
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
×
708
      qError("stmt async output failed");
×
709
    }
710
  }
711

712
  qInfo("stmt bind thread stopped");
×
713

714
  return NULL;
×
715
}
716

717
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
×
718
  TdThreadAttr thAttr;
719
  if (taosThreadAttrInit(&thAttr) != 0) {
×
720
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
721
  }
722
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
×
723
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
724
  }
725

726
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
×
727
    terrno = TAOS_SYSTEM_ERROR(errno);
×
728
    STMT_ERR_RET(terrno);
×
729
  }
730

731
  pStmt->bindThreadInUse = true;
×
732

733
  (void)taosThreadAttrDestroy(&thAttr);
×
734
  return TSDB_CODE_SUCCESS;
×
735
}
736

737
static int32_t stmtInitQueue(STscStmt2* pStmt) {
×
738
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
×
739
  pStmt->queue.tail = pStmt->queue.head;
×
740

741
  return TSDB_CODE_SUCCESS;
×
742
}
743

744
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
×
745
  pTblBuf->buffUnit = sizeof(SStmtQNode);
×
746
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
×
747
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
×
748
  if (NULL == pTblBuf->pBufList) {
×
749
    return terrno;
×
750
  }
751
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
752
  if (NULL == buff) {
×
753
    return terrno;
×
754
  }
755

756
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
×
757
    return terrno;
×
758
  }
759

760
  pTblBuf->pCurBuff = buff;
×
761
  pTblBuf->buffIdx = 1;
×
762
  pTblBuf->buffOffset = 0;
×
763

764
  return TSDB_CODE_SUCCESS;
×
765
}
766

767
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
×
768
  STscObj*   pObj = (STscObj*)taos;
×
769
  STscStmt2* pStmt = NULL;
×
770
  int32_t    code = 0;
×
771

772
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
×
773
  if (NULL == pStmt) {
×
774
    return NULL;
×
775
  }
776

777
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
778
  if (NULL == pStmt->sql.pTableCache) {
×
779
    taosMemoryFree(pStmt);
×
780
    return NULL;
×
781
  }
782

783
  pStmt->taos = pObj;
×
784
  pStmt->bInfo.needParse = true;
×
785
  pStmt->sql.status = STMT_INIT;
×
786
  pStmt->errCode = TSDB_CODE_SUCCESS;
×
787

788
  if (NULL != pOptions) {
×
789
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
×
790
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
×
791
      pStmt->stbInterlaceMode = true;
×
792
    }
793

794
    pStmt->reqid = pOptions->reqid;
×
795
  }
796

797
  if (pStmt->stbInterlaceMode) {
×
798
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
×
799
    pStmt->sql.siInfo.acctId = taos->acctId;
×
800
    pStmt->sql.siInfo.dbname = taos->db;
×
801
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
×
802
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
×
803
    if (NULL == pStmt->sql.siInfo.pTableHash) {
×
804
      (void)stmtClose(pStmt);
×
805
      return NULL;
×
806
    }
807
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
×
808
    if (NULL == pStmt->sql.siInfo.pTableCols) {
×
809
      terrno = terrno;
×
810
      (void)stmtClose(pStmt);
×
811
      return NULL;
×
812
    }
813

814
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
×
815
    if (TSDB_CODE_SUCCESS == code) {
×
816
      code = stmtInitQueue(pStmt);
×
817
    }
818
    if (TSDB_CODE_SUCCESS == code) {
×
819
      code = stmtStartBindThread(pStmt);
×
820
    }
821
    if (TSDB_CODE_SUCCESS != code) {
×
822
      terrno = code;
×
823
      (void)stmtClose(pStmt);
×
824
      return NULL;
×
825
    }
826
  }
827

828
  pStmt->sql.siInfo.tableColsReady = true;
×
829
  if (pStmt->options.asyncExecFn) {
×
830
    if (tsem_init(&pStmt->asyncQuerySem, 0, 1) != 0) {
×
831
      terrno = TAOS_SYSTEM_ERROR(errno);
×
832
      (void)stmtClose(pStmt);
×
833
      return NULL;
×
834
    }
835
  }
836
  pStmt->semWaited = false;
×
837

838
  STMT_LOG_SEQ(STMT_INIT);
×
839

840
  tscDebug("stmt:%p initialized", pStmt);
×
841

842
  return pStmt;
×
843
}
844

845
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
846
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
847

848
  STMT_DLOG("start to set dbName: %s", dbName);
×
849

850
  pStmt->db = taosStrdup(dbName);
×
851
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
852

853
  // The SQL statement specifies a database name, overriding the previously specified database
854
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
855
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
×
856
  if (pStmt->exec.pRequest->pDb == NULL) {
×
857
    return terrno;
×
858
  }
859
  return TSDB_CODE_SUCCESS;
×
860
}
861

862
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
×
863
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
864

865
  STMT_DLOG_E("start to prepare");
×
866

867
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
868
    return pStmt->errCode;
×
869
  }
870

871
  if (pStmt->sql.status >= STMT_PREPARE) {
×
872
    STMT_ERR_RET(stmtResetStmt(pStmt));
×
873
  }
874

875
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
×
876

877
  if (length <= 0) {
×
878
    length = strlen(sql);
×
879
  }
880

881
  pStmt->sql.sqlStr = taosStrndup(sql, length);
×
882
  if (!pStmt->sql.sqlStr) {
×
883
    return terrno;
×
884
  }
885
  pStmt->sql.sqlLen = length;
×
886
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
×
887

888
  char* dbName = NULL;
×
889
  if (qParseDbName(sql, length, &dbName)) {
×
890
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
891
    taosMemoryFreeClear(dbName);
×
892
  }
893

894
  return TSDB_CODE_SUCCESS;
×
895
}
896

897
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
×
898
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
899
  if (!pSrc) {
×
900
    return terrno;
×
901
  }
902
  STableDataCxt* pDst = NULL;
×
903

904
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
×
905
  pStmt->sql.siInfo.pDataCtx = pDst;
×
906

907
  SArray* pTblCols = NULL;
×
908
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
×
909
    pTblCols = taosArrayInit(20, POINTER_BYTES);
×
910
    if (NULL == pTblCols) {
×
911
      return terrno;
×
912
    }
913

914
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
915
      return terrno;
×
916
    }
917
  }
918

919
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
×
920

921
  return TSDB_CODE_SUCCESS;
×
922
}
923

924
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
×
925
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
926

927
  STMT_DLOG_E("start is insert");
×
928

929
  if (pStmt->sql.type) {
×
930
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
×
931
  } else {
932
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
×
933
  }
934

935
  return TSDB_CODE_SUCCESS;
×
936
}
937

938
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
×
939
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
940

941
  int64_t startUs = taosGetTimestampUs();
×
942

943
  STMT_DLOG("start to set tbName: %s", tbName);
×
944

945
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
946
    return pStmt->errCode;
×
947
  }
948

949
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
×
950

951
  int32_t insert = 0;
×
952
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
×
953
  if (0 == insert) {
×
954
    tscError("set tb name not available for none insert statement");
×
955
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
956
  }
957

958
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
×
959
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
960

961
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
×
962
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
963
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
×
964

965
    STMT_ERR_RET(stmtGetFromCache(pStmt));
×
966

967
    if (pStmt->bInfo.needParse) {
×
968
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
×
969
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
×
970

971
      STMT_ERR_RET(stmtParseSql(pStmt));
×
972
    }
973
  } else {
974
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
×
975
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
×
976
    pStmt->exec.pRequest->requestId++;
×
977
    pStmt->bInfo.needParse = false;
×
978
  }
979

980
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
981
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
982
  }
983

984
  int64_t startUs2 = taosGetTimestampUs();
×
985
  pStmt->stat.setTbNameUs += startUs2 - startUs;
×
986

987
  return TSDB_CODE_SUCCESS;
×
988
}
989

990
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) {
×
991
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
992

993
  STMT_DLOG_E("start to set tbTags");
×
994

995
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
996
    return pStmt->errCode;
×
997
  }
998

999
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
×
1000

1001
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1002
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1003
    pStmt->bInfo.needParse = false;
×
1004
  }
1005
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1006

1007
  if (pStmt->bInfo.needParse) {
×
1008
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1009
  }
1010
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
1011
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1012
  }
1013

1014
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
×
1015
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
×
1016
    tscWarn("no tags or cols bound in sql, will not bound tags");
×
1017
    return TSDB_CODE_SUCCESS;
×
1018
  }
1019

1020
  STableDataCxt** pDataBlock =
1021
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1022
  if (NULL == pDataBlock) {
×
1023
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1024
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1025
  }
1026

1027
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
×
1028
    return TSDB_CODE_SUCCESS;
×
1029
  }
1030

1031
  tscDebug("start to bind stmt tag values");
×
1032
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
×
1033
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1034
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1035

1036
  return TSDB_CODE_SUCCESS;
×
1037
}
1038

1039
static int stmtFetchTagFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1040
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1041
    return pStmt->errCode;
×
1042
  }
1043

1044
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1045
    tscError("invalid operation to get query tag fileds");
×
1046
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1047
  }
1048

1049
  STableDataCxt** pDataBlock =
1050
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1051
  if (NULL == pDataBlock) {
×
1052
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1053
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1054
  }
1055

1056
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
×
1057

1058
  return TSDB_CODE_SUCCESS;
×
1059
}
1060

1061
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1062
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1063
    return pStmt->errCode;
×
1064
  }
1065

1066
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1067
    tscError("invalid operation to get query column fileds");
×
1068
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1069
  }
1070

1071
  STableDataCxt** pDataBlock = NULL;
×
1072

1073
  if (pStmt->sql.stbInterlaceMode) {
×
1074
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1075
  } else {
1076
    pDataBlock =
1077
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1078
    if (NULL == pDataBlock) {
×
1079
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1080
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1081
    }
1082
  }
1083

1084
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1085

1086
  return TSDB_CODE_SUCCESS;
×
1087
}
1088

1089
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
×
1090
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1091
    return pStmt->errCode;
×
1092
  }
1093

1094
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1095
    tscError("invalid operation to get query column fileds");
×
1096
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1097
  }
1098

1099
  STableDataCxt** pDataBlock = NULL;
×
1100

1101
  if (pStmt->sql.stbInterlaceMode) {
×
1102
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1103
  } else {
1104
    pDataBlock =
1105
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1106
    if (NULL == pDataBlock) {
×
1107
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1108
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1109
    }
1110
  }
1111

1112
  STMT_ERR_RET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.preCtbname, fieldNum, fields));
×
1113
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE) {
×
1114
    pStmt->bInfo.needParse = true;
×
1115
    qDestroyStmtDataBlock(*pDataBlock);
×
1116
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
×
1117
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
1118
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1119
    }
1120
  }
1121

1122
  return TSDB_CODE_SUCCESS;
×
1123
}
1124
/*
1125
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1126
  while (true) {
1127
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1128
      pStmt->exec.smInfo.pColIdx = 0;
1129
    }
1130

1131
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1132
      taosUsleep(1);
1133
      continue;
1134
    }
1135

1136
    *idx = pStmt->exec.smInfo.pColIdx;
1137
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1138
  }
1139
}
1140
*/
1141
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
×
1142
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
×
1143
    pStmt->sql.siInfo.pVgroupHash =
×
1144
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
×
1145
  }
1146
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
×
1147
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
×
1148
  }
1149

1150
  if (NULL == pStmt->sql.siInfo.pRequest) {
×
1151
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
×
1152
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1153

1154
    if (pStmt->reqid != 0) {
×
1155
      pStmt->reqid++;
×
1156
    }
1157
    pStmt->exec.pRequest->syncQuery = true;
×
1158

1159
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
×
1160
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
×
1161
  }
1162

1163
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
×
1164
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
×
1165
    pStmt->sql.siInfo.tbFromHash = true;
×
1166
  }
1167

1168
  if (0 == pStmt->sql.siInfo.firstName[0]) {
×
1169
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
×
1170
  }
1171

1172
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
×
1173
  param->next = NULL;
×
1174

1175
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
×
1176

1177
  stmtEnqueue(pStmt, param);
×
1178

1179
  return TSDB_CODE_SUCCESS;
×
1180
}
1181

1182
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1183
  while (true) {
1184
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
×
1185
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
×
1186
      break;
×
1187
    } else {
1188
      SArray* pTblCols = NULL;
×
1189
      for (int32_t i = 0; i < 100; i++) {
×
1190
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1191
        if (NULL == pTblCols) {
×
1192
          return terrno;
×
1193
        }
1194

1195
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1196
          return terrno;
×
1197
        }
1198
      }
1199
    }
1200
  }
1201

1202
  return TSDB_CODE_SUCCESS;
×
1203
}
1204

1205
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
×
1206
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
×
1207
    return TSDB_CODE_SUCCESS;
×
1208
  }
1209

1210
  uint64_t uid = pStmt->bInfo.tbUid;
×
1211
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
×
1212

1213
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
×
1214
    return TSDB_CODE_SUCCESS;
×
1215
  }
1216

1217
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1218
  if (!pSrc) {
×
1219
    return terrno;
×
1220
  }
1221
  STableDataCxt* pDst = NULL;
×
1222

1223
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
×
1224

1225
  SStmtTableCache cache = {
×
1226
      .pDataCtx = pDst,
1227
      .boundTags = pStmt->bInfo.boundTags,
×
1228
  };
1229

1230
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
×
1231
    return terrno;
×
1232
  }
1233

1234
  if (pStmt->sql.autoCreateTbl) {
×
1235
    pStmt->bInfo.tagsCached = true;
×
1236
  } else {
1237
    pStmt->bInfo.boundTags = NULL;
×
1238
  }
1239

1240
  return TSDB_CODE_SUCCESS;
×
1241
}
1242

1243
static int stmtAddBatch2(TAOS_STMT2* stmt) {
×
1244
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1245

1246
  int64_t startUs = taosGetTimestampUs();
×
1247

1248
  STMT_DLOG_E("start to add batch");
×
1249

1250
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1251
    return pStmt->errCode;
×
1252
  }
1253

1254
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
×
1255

1256
  if (pStmt->sql.stbInterlaceMode) {
×
1257
    int64_t startUs2 = taosGetTimestampUs();
×
1258
    pStmt->stat.addBatchUs += startUs2 - startUs;
×
1259

1260
    pStmt->sql.siInfo.tableColsReady = false;
×
1261

1262
    SStmtQNode* param = NULL;
×
1263
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
×
1264
    param->restoreTbCols = true;
×
1265
    param->next = NULL;
×
1266

1267
    stmtEnqueue(pStmt, param);
×
1268

1269
    return TSDB_CODE_SUCCESS;
×
1270
  }
1271

1272
  STMT_ERR_RET(stmtCacheBlock(pStmt));
×
1273

1274
  return TSDB_CODE_SUCCESS;
×
1275
}
1276
/*
1277
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1278
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1279
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1280
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1281

1282
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1283
  pRes->fields = taosMemoryMalloc(size);
1284
  pRes->userFields = taosMemoryMalloc(size);
1285
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1286
    STMT_ERR_RET(terrno);
1287
  }
1288
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1289
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1290

1291
  return TSDB_CODE_SUCCESS;
1292
}
1293

1294
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1295
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1296
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1297

1298
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1299
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1300

1301
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1302
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1303
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1304
      STMT_ERR_RET(terrno);
1305
    }
1306
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1307
  }
1308

1309
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1310
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1311
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1312
      STMT_ERR_RET(terrno);
1313
    }
1314
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1315
  }
1316

1317
  return TSDB_CODE_SUCCESS;
1318
}
1319
*/
1320
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) {
×
1321
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1322
  int32_t    code = 0;
×
1323

1324
  int64_t startUs = taosGetTimestampUs();
×
1325

1326
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
×
1327

1328
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1329
    return pStmt->errCode;
×
1330
  }
1331

1332
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
×
1333

1334
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1335
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1336
    pStmt->bInfo.needParse = false;
×
1337
  }
1338

1339
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1340
    taos_free_result(pStmt->exec.pRequest);
×
1341
    pStmt->exec.pRequest = NULL;
×
1342
  }
1343

1344
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1345

1346
  if (pStmt->bInfo.needParse) {
×
1347
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1348
  }
1349

1350
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1351
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
×
1352

1353
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
×
1354
                         .acctId = pStmt->taos->acctId,
×
1355
                         .db = pStmt->exec.pRequest->pDb,
×
1356
                         .topicQuery = false,
1357
                         .pSql = pStmt->sql.sqlStr,
×
1358
                         .sqlLen = pStmt->sql.sqlLen,
×
1359
                         .pMsg = pStmt->exec.pRequest->msgBuf,
×
1360
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1361
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
×
1362
                         .pStmtCb = NULL,
1363
                         .pUser = pStmt->taos->user};
×
1364
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
×
1365
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
×
1366

1367
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
×
1368

1369
    if (pStmt->sql.pQuery->haveResultSet) {
×
1370
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
×
1371
                                    pStmt->sql.pQuery->numOfResCols));
1372
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
×
1373
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
×
1374
    }
1375

1376
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
×
1377
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
×
1378
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
×
1379

1380
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1381
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1382
    // }
1383

1384
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1385

1386
    return TSDB_CODE_SUCCESS;
×
1387
  }
1388

1389
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
1390
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1391
  }
1392

1393
  STableDataCxt** pDataBlock = NULL;
×
1394

1395
  if (pStmt->exec.pCurrBlock) {
×
1396
    pDataBlock = &pStmt->exec.pCurrBlock;
×
1397
  } else {
1398
    pDataBlock =
1399
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1400
    if (NULL == pDataBlock) {
×
1401
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1402
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1403
    }
1404
    pStmt->exec.pCurrBlock = *pDataBlock;
×
1405
    if (pStmt->sql.stbInterlaceMode) {
×
1406
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
×
1407
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
×
1408
    }
1409
  }
1410

1411
  int64_t startUs2 = taosGetTimestampUs();
×
1412
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
×
1413

1414
  SStmtQNode* param = NULL;
×
1415
  if (pStmt->sql.stbInterlaceMode) {
×
1416
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
×
1417
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
×
1418
    taosArrayClear(param->tblData.aCol);
×
1419

1420
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1421

1422
    param->restoreTbCols = false;
×
1423
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
×
1424
  }
1425

1426
  int64_t startUs3 = taosGetTimestampUs();
×
1427
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
×
1428

1429
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
×
1430

1431
  if (colIdx < 0) {
×
1432
    if (pStmt->sql.stbInterlaceMode) {
×
1433
      (*pDataBlock)->pData->flags = 0;
×
1434
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1435
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
×
1436
    } else {
1437
      code =
1438
          qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
×
1439
    }
1440

1441
    if (code) {
×
1442
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
×
1443
      STMT_ERR_RET(code);
×
1444
    }
1445
  } else {
1446
    if (pStmt->sql.stbInterlaceMode) {
×
1447
      tscError("bind single column not allowed in stb insert mode");
×
1448
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1449
    }
1450

1451
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1452
      tscError("bind column index not in sequence");
×
1453
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1454
    }
1455

1456
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1457

1458
    if (0 == colIdx) {
×
1459
      pStmt->bInfo.sBindRowNum = bind->num;
×
1460
    }
1461

1462
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1463
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1464
    if (code) {
×
1465
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1466
      STMT_ERR_RET(code);
×
1467
    }
1468
  }
1469

1470
  int64_t startUs4 = taosGetTimestampUs();
×
1471
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
×
1472

1473
  if (pStmt->sql.stbInterlaceMode) {
×
1474
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
×
1475
  } else {
1476
    STMT_ERR_RET(stmtAddBatch2(pStmt));
×
1477
  }
1478

1479
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
×
1480

1481
  return TSDB_CODE_SUCCESS;
×
1482
}
1483
/*
1484
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1485
  tscDebug("stmt start to update tbUid, blockNum: %d", pRsp->nBlocks);
1486

1487
  int32_t code = 0;
1488
  int32_t finalCode = 0;
1489
  size_t  keyLen = 0;
1490
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1491
  while (pIter) {
1492
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1493
    char*          key = taosHashGetKey(pIter, &keyLen);
1494

1495
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1496
    if (pMeta->uid) {
1497
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1498
      continue;
1499
    }
1500

1501
    SSubmitBlkRsp* blkRsp = NULL;
1502
    int32_t        i = 0;
1503
    for (; i < pRsp->nBlocks; ++i) {
1504
      blkRsp = pRsp->pBlocks + i;
1505
      if (strlen(blkRsp->tblFName) != keyLen) {
1506
        continue;
1507
      }
1508

1509
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1510
        continue;
1511
      }
1512

1513
      break;
1514
    }
1515

1516
    if (i < pRsp->nBlocks) {
1517
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1518
               blkRsp->uid);
1519

1520
      pMeta->uid = blkRsp->uid;
1521
      pStmt->bInfo.tbUid = blkRsp->uid;
1522
    } else {
1523
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1524
      if (NULL == pStmt->pCatalog) {
1525
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1526
        if (code) {
1527
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1528
          finalCode = code;
1529
          continue;
1530
        }
1531
      }
1532

1533
      code = stmtCreateRequest(pStmt);
1534
      if (code) {
1535
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1536
        finalCode = code;
1537
        continue;
1538
      }
1539

1540
      STableMeta*      pTableMeta = NULL;
1541
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1542
                               .requestId = pStmt->exec.pRequest->requestId,
1543
                               .requestObjRefId = pStmt->exec.pRequest->self,
1544
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1545
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1546

1547
      pStmt->stat.ctgGetTbMetaNum++;
1548

1549
      taos_free_result(pStmt->exec.pRequest);
1550
      pStmt->exec.pRequest = NULL;
1551

1552
      if (code || NULL == pTableMeta) {
1553
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1554
        finalCode = code;
1555
        taosMemoryFree(pTableMeta);
1556
        continue;
1557
      }
1558

1559
      pMeta->uid = pTableMeta->uid;
1560
      pStmt->bInfo.tbUid = pTableMeta->uid;
1561
      taosMemoryFree(pTableMeta);
1562
    }
1563

1564
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1565
  }
1566

1567
  return finalCode;
1568
}
1569
*/
1570
/*
1571
int stmtStaticModeExec(TAOS_STMT* stmt) {
1572
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1573
  int32_t     code = 0;
1574
  SSubmitRsp* pRsp = NULL;
1575
  if (pStmt->sql.staticMode) {
1576
    return TSDB_CODE_TSC_STMT_API_ERROR;
1577
  }
1578

1579
  STMT_DLOG_E("start to exec");
1580

1581
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1582

1583
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1584
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1585

1586
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1587

1588
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1589
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1590
    if (code) {
1591
      pStmt->exec.pRequest->code = code;
1592
    } else {
1593
      tFreeSSubmitRsp(pRsp);
1594
      STMT_ERR_RET(stmtResetStmt(pStmt));
1595
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1596
    }
1597
  }
1598

1599
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1600

1601
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1602
  pStmt->affectedRows += pStmt->exec.affectedRows;
1603

1604
_return:
1605

1606
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1607

1608
  tFreeSSubmitRsp(pRsp);
1609

1610
  ++pStmt->sql.runTimes;
1611

1612
  STMT_RET(code);
1613
}
1614
*/
1615

1616
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
1617
  const STscObj* pTscObj = pRequest->pTscObj;
×
1618

1619
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
1620
  if (*pCxt == NULL) {
×
1621
    return terrno;
×
1622
  }
1623

1624
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
1625
                           .requestRid = pRequest->self,
×
1626
                           .acctId = pTscObj->acctId,
×
1627
                           .db = pRequest->pDb,
×
1628
                           .topicQuery = false,
1629
                           .pSql = pRequest->sqlstr,
×
1630
                           .sqlLen = pRequest->sqlLen,
×
1631
                           .pMsg = pRequest->msgBuf,
×
1632
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1633
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
1634
                           .pStmtCb = NULL,
1635
                           .pUser = pTscObj->user,
×
1636
                           .pEffectiveUser = pRequest->effectiveUser,
×
1637
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
1638
                           .enableSysInfo = pTscObj->sysInfo,
×
1639
                           .async = true,
1640
                           .svrVer = pTscObj->sVer,
×
1641
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
1642
                           .allocatorId = pRequest->allocatorRefId,
×
1643
                           .parseSqlFp = clientParseSql,
1644
                           .parseSqlParam = pWrapper};
1645
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
1646
  (*pCxt)->biMode = biMode;
×
1647
  return TSDB_CODE_SUCCESS;
×
1648
}
1649

1650
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
1651
  STscStmt2*        pStmt = userdata;
×
1652
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1653

1654
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1655
  pStmt->affectedRows += pStmt->exec.affectedRows;
×
1656

1657
  fp(pStmt->options.userdata, res, code);
×
1658

1659
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1660
    taosUsleep(1);
×
1661
  }
1662
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
1663
  ++pStmt->sql.runTimes;
×
1664

1665
  if (tsem_post(&pStmt->asyncQuerySem) != 0) {
×
1666
    tscError("failed to post asyncQuerySem");
×
1667
  }
1668
}
×
1669

1670
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
×
1671
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1672
  int32_t    code = 0;
×
1673
  int64_t    startUs = taosGetTimestampUs();
×
1674

1675
  STMT_DLOG_E("start to exec");
×
1676

1677
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1678
    return pStmt->errCode;
×
1679
  }
1680

1681
  if (pStmt->sql.stbInterlaceMode) {
×
1682
    STMT_ERR_RET(stmtAddBatch2(pStmt));
×
1683
  }
1684

1685
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
×
1686

1687
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
1688
    if (pStmt->sql.stbInterlaceMode) {
×
1689
      int64_t startTs = taosGetTimestampUs();
×
1690
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
×
1691
        taosUsleep(1);
×
1692
      }
1693
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
×
1694

1695
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
×
1696
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1697
      pStmt->sql.siInfo.pVgroupHash = NULL;
×
1698
      pStmt->sql.siInfo.pVgroupList = NULL;
×
1699
    } else {
1700
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
×
1701
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
×
1702

1703
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
×
1704

1705
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
×
1706
    }
1707
  }
1708

1709
  SRequestObj*      pRequest = pStmt->exec.pRequest;
×
1710
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1711

1712
  if (!fp) {
×
1713
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
×
1714

1715
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
×
1716
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1717
      if (code) {
×
1718
        pStmt->exec.pRequest->code = code;
×
1719
      } else {
1720
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1721
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1722
      }
1723
    }
1724

1725
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
×
1726

1727
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1728
    if (affected_rows) {
×
1729
      *affected_rows = pStmt->exec.affectedRows;
×
1730
    }
1731
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
1732

1733
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1734
      taosUsleep(1);
×
1735
    }
1736

1737
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
×
1738

1739
    ++pStmt->sql.runTimes;
×
1740
  } else {
1741
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
1742
    if (pWrapper == NULL) {
×
1743
      code = terrno;
×
1744
    } else {
1745
      pWrapper->pRequest = pRequest;
×
1746
      pRequest->pWrapper = pWrapper;
×
1747
    }
1748
    if (TSDB_CODE_SUCCESS == code) {
×
1749
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
1750
    }
1751
    pRequest->syncQuery = false;
×
1752
    pRequest->body.queryFp = asyncQueryCb;
×
1753
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
1754

1755
    pStmt->semWaited = false;
×
1756
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
1757
  }
1758

1759
_return:
×
1760
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
×
1761

1762
  STMT_RET(code);
×
1763
}
1764

1765
int stmtClose2(TAOS_STMT2* stmt) {
×
1766
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1767

1768
  STMT_DLOG_E("start to free stmt");
×
1769

1770
  pStmt->queue.stopQueue = true;
×
1771

1772
  if (pStmt->bindThreadInUse) {
×
1773
    (void)taosThreadJoin(pStmt->bindThread, NULL);
×
1774
    pStmt->bindThreadInUse = false;
×
1775
  }
1776

1777
  if (pStmt->options.asyncExecFn && !pStmt->semWaited) {
×
1778
    if (tsem_wait(&pStmt->asyncQuerySem) != 0) {
×
1779
      tscError("failed to wait asyncQuerySem");
×
1780
    }
1781
  }
1782

1783
  STMT_DLOG("stmt %p closed, stbInterlaceMode: %d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
×
1784
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1785
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1786
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1787
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1788
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1789
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1790
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1791
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1792
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1793

1794
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
1795

1796
  if (pStmt->options.asyncExecFn) {
×
1797
    if (tsem_destroy(&pStmt->asyncQuerySem) != 0) {
×
1798
      tscError("failed to destroy asyncQuerySem");
×
1799
    }
1800
  }
1801
  taosMemoryFree(stmt);
×
1802

1803
  return TSDB_CODE_SUCCESS;
×
1804
}
1805

1806
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
1807
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1808

1809
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
1810
    return (char*)tstrerror(terrno);
×
1811
  }
1812

1813
  pStmt->exec.pRequest->code = terrno;
×
1814

1815
  return taos_errstr(pStmt->exec.pRequest);
×
1816
}
1817
/*
1818
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
1819

1820
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
1821
*/
1822
int stmtGetTagFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_E** fields) {
×
1823
  int32_t    code = 0;
×
1824
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1825
  int32_t    preCode = pStmt->errCode;
×
1826

1827
  STMT_DLOG_E("start to get tag fields");
×
1828

1829
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1830
    return pStmt->errCode;
×
1831
  }
1832

1833
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1834
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1835
  }
1836

1837
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1838

1839
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1840
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1841
    pStmt->bInfo.needParse = false;
×
1842
  }
1843

1844
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1845
    taos_free_result(pStmt->exec.pRequest);
×
1846
    pStmt->exec.pRequest = NULL;
×
1847
  }
1848

1849
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1850

1851
  if (pStmt->bInfo.needParse) {
×
1852
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1853
  }
1854

1855
  STMT_ERRI_JRET(stmtFetchTagFields2(stmt, nums, fields));
×
1856

1857
_return:
×
1858

1859
  pStmt->errCode = preCode;
×
1860

1861
  return code;
×
1862
}
1863

1864
int stmtParseColFields2(TAOS_STMT2* stmt) {
×
1865
  int32_t    code = 0;
×
1866
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1867
  int32_t    preCode = pStmt->errCode;
×
1868

1869
  STMT_DLOG_E("start to get col fields");
×
1870

1871
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1872
    return pStmt->errCode;
×
1873
  }
1874

1875
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1876
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1877
  }
1878

1879
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1880

1881
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1882
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1883
    pStmt->bInfo.needParse = false;
×
1884
  }
1885

1886
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1887
    taos_free_result(pStmt->exec.pRequest);
×
1888
    pStmt->exec.pRequest = NULL;
×
1889
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1890
  }
1891

1892
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1893

1894
  if (pStmt->bInfo.needParse) {
×
1895
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1896
  }
1897

1898
_return:
×
1899

1900
  pStmt->errCode = preCode;
×
1901

1902
  return code;
×
1903
}
1904

1905
int stmtGetColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_E** fields) {
×
1906
  int32_t code = stmtParseColFields2(stmt);
×
1907
  if (code != TSDB_CODE_SUCCESS) {
×
1908
    return code;
×
1909
  }
1910

1911
  return stmtFetchColFields2(stmt, nums, fields);
×
1912
}
1913

1914
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
×
1915
  int32_t code = stmtParseColFields2(stmt);
×
1916
  if (code != TSDB_CODE_SUCCESS) {
×
1917
    return code;
×
1918
  }
1919

1920
  return stmtFetchStbColFields2(stmt, nums, fields);
×
1921
}
1922

1923
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
1924
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1925

1926
  STMT_DLOG_E("start to get param num");
×
1927

1928
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1929
    return pStmt->errCode;
×
1930
  }
1931

1932
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1933

1934
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1935
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1936
    pStmt->bInfo.needParse = false;
×
1937
  }
1938

1939
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1940
    taos_free_result(pStmt->exec.pRequest);
×
1941
    pStmt->exec.pRequest = NULL;
×
1942
  }
1943

1944
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1945

1946
  if (pStmt->bInfo.needParse) {
×
1947
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1948
  }
1949

1950
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1951
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1952
  } else {
1953
    STMT_ERR_RET(stmtFetchColFields2(stmt, nums, NULL));
×
1954
  }
1955

1956
  return TSDB_CODE_SUCCESS;
×
1957
}
1958

1959
int stmtGetParamTbName(TAOS_STMT2* stmt, int* nums) {
×
1960
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1961
  int32_t    code = 0;
×
1962
  int32_t    preCode = pStmt->errCode;
×
1963

1964
  STMT_DLOG_E("start to get param num");
×
1965

1966
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1967
    return pStmt->errCode;
×
1968
  }
1969

1970
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1971

1972
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1973
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1974
    pStmt->bInfo.needParse = false;
×
1975
  }
1976

1977
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1978
    taos_free_result(pStmt->exec.pRequest);
×
1979
    pStmt->exec.pRequest = NULL;
×
1980
  }
1981

1982
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1983

1984
  if (pStmt->bInfo.needParse) {
×
1985
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1986
  }
1987

1988
  *nums = STMT_TYPE_MULTI_INSERT == pStmt->sql.type ? 1 : 0;
×
1989

1990
_return:
×
1991
  if (TSDB_CODE_TSC_STMT_TBNAME_ERROR == code) {
×
1992
    *nums = 1;
×
1993
    code = TSDB_CODE_SUCCESS;
×
1994
  }
1995

1996
  pStmt->errCode = preCode;
×
1997
  return code;
×
1998
}
1999
/*
2000
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
2001
  STscStmt2* pStmt = (STscStmt2*)stmt;
2002

2003
  STMT_DLOG_E("start to get param");
2004

2005
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2006
    return pStmt->errCode;
2007
  }
2008

2009
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2010
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
2011
  }
2012

2013
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2014

2015
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2016
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
2017
    pStmt->bInfo.needParse = false;
2018
  }
2019

2020
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2021
    taos_free_result(pStmt->exec.pRequest);
2022
    pStmt->exec.pRequest = NULL;
2023
  }
2024

2025
  STMT_ERR_RET(stmtCreateRequest(pStmt));
2026

2027
  if (pStmt->bInfo.needParse) {
2028
    STMT_ERR_RET(stmtParseSql(pStmt));
2029
  }
2030

2031
  int32_t       nums = 0;
2032
  TAOS_FIELD_E* pField = NULL;
2033
  STMT_ERR_RET(stmtFetchColFields(stmt, &nums, &pField));
2034
  if (idx >= nums) {
2035
    tscError("idx %d is too big", idx);
2036
    taosMemoryFree(pField);
2037
    STMT_ERR_RET(TSDB_CODE_INVALID_PARA);
2038
  }
2039

2040
  *type = pField[idx].type;
2041
  *bytes = pField[idx].bytes;
2042

2043
  taosMemoryFree(pField);
2044

2045
  return TSDB_CODE_SUCCESS;
2046
}
2047
*/
2048
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
×
2049
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2050

2051
  STMT_DLOG_E("start to use result");
×
2052

2053
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
2054
    tscError("useResult only for query statement");
×
2055
    return NULL;
×
2056
  }
2057

2058
  return pStmt->exec.pRequest;
×
2059
}
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