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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 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
  int i = 0;
×
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
×
44
    if (i < 10) {
×
45
      taosUsleep(1);
×
46
      i++;
×
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
×
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
×
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
×
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
×
56
    return false;
×
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
×
59
  SStmtQNode* node = pStmt->queue.head->next;
×
60
  pStmt->queue.head = pStmt->queue.head->next;
×
61
  *param = node;
×
62

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

65
  return true;
×
66
}
67

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

72
  pStmt->stat.bindDataNum++;
×
73

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

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

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

100
  return code;
×
101
}
102

103
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
×
104
  int32_t code = 0;
×
105

106
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
×
107
    STMT_LOG_SEQ(newStatus);
×
108
  }
109

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

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

174
  STMT_ERR_RET(code);
×
175

176
  pStmt->sql.status = newStatus;
×
177

178
  return TSDB_CODE_SUCCESS;
×
179
}
180

181
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
×
182
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
183

184
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
×
185

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

191
  *tbName = pStmt->bInfo.tbName;
×
192

193
  return TSDB_CODE_SUCCESS;
×
194
}
195

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

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

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

214
  if (!pStmt->bInfo.tagsCached) {
×
215
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
×
216
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
×
217
  }
218

219
  pStmt->bInfo.boundTags = tags;
×
220
  pStmt->bInfo.tagsCached = false;
×
221
  pStmt->bInfo.tbNameFlag = tbNameFlag;
×
222
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
×
223

224
  return TSDB_CODE_SUCCESS;
×
225
}
226

227
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
×
228
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
229

230
  pStmt->sql.pVgHash = pVgHash;
×
231
  pStmt->exec.pBlockHash = pBlockHash;
×
232

233
  return TSDB_CODE_SUCCESS;
×
234
}
235

236
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
×
237
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, uint8_t tbNameFlag) {
238
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
239

240
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
×
241
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
×
242

243
  pStmt->sql.autoCreateTbl = autoCreateTbl;
×
244

245
  return TSDB_CODE_SUCCESS;
×
246
}
247

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

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

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

257
  return TSDB_CODE_SUCCESS;
×
258
}
259

260
static int32_t stmtParseSql(STscStmt2* pStmt) {
×
261
  pStmt->exec.pCurrBlock = NULL;
×
262

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

270
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
271

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

276
  pStmt->bInfo.needParse = false;
×
277

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

285
    return TSDB_CODE_SUCCESS;
×
286
  }
287

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

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

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

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

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

315
  return TSDB_CODE_SUCCESS;
×
316
}
317

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

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

336
  return TSDB_CODE_SUCCESS;
×
337
}
338

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

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

353
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
×
354
  pQueue->qRemainNum = 0;
×
355
  pQueue->head->next = NULL;
×
356
}
357

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

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

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

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

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

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

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

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

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

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

415
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
416

417
  return TSDB_CODE_SUCCESS;
×
418
}
419

420
static void stmtFreeTbBuf(void* buf) {
×
421
  void* pBuf = *(void**)buf;
×
422
  taosMemoryFree(pBuf);
×
423
}
×
424

425
static void stmtFreeTbCols(void* buf) {
×
426
  SArray* pCols = *(SArray**)buf;
×
427
  taosArrayDestroy(pCols);
×
428
}
×
429

430
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
×
431
  STMT_DLOG_E("start to free SQL info");
×
432

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

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

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

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

459
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
×
460
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
461

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

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

473
  STMT_DLOG_E("end to free SQL info");
×
474

475
  return TSDB_CODE_SUCCESS;
×
476
}
477

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

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

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

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

500
  *vgId = vgInfo.vgId;
×
501

502
  return TSDB_CODE_SUCCESS;
×
503
}
504

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

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

512
  return TSDB_CODE_SUCCESS;
×
513
}
514

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

522
  pStmt->bInfo.needParse = true;
×
523
  pStmt->bInfo.inExecCache = false;
×
524

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

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

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

538
  if (NULL == pStmt->pCatalog) {
×
539
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
×
540
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
×
541
  }
542

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

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

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

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

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

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

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

572
      return TSDB_CODE_SUCCESS;
×
573
    }
574

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

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

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

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

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

595
    STMT_ERR_RET(code);
×
596
  }
597

598
  STMT_ERR_RET(code);
×
599

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

606
  taosMemoryFree(pTableMeta);
×
607

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

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

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

615
    return TSDB_CODE_SUCCESS;
×
616
  }
617

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

624
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
625
    }
626

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

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

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

637
    return TSDB_CODE_SUCCESS;
×
638
  }
639

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

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

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

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

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

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

662
    return TSDB_CODE_SUCCESS;
×
663
  }
664

665
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
666

667
  return TSDB_CODE_SUCCESS;
×
668
}
669

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

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

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

680
  return TSDB_CODE_SUCCESS;
×
681
}
682

683
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
×
684
  SStmtQNode* pParam = (SStmtQNode*)param;
×
685

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

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

706
static void* stmtBindThreadFunc(void* param) {
×
707
  setThreadName("stmtBind");
×
708

709
  qInfo("stmt bind thread started");
×
710

711
  STscStmt2* pStmt = (STscStmt2*)param;
×
712

713
  while (true) {
×
714
    if (pStmt->queue.stopQueue) {
×
715
      break;
×
716
    }
717

718
    SStmtQNode* asyncParam = NULL;
×
719
    if (!stmtDequeue(pStmt, &asyncParam)) {
×
720
      continue;
×
721
    }
722

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

728
  qInfo("stmt bind thread stopped");
×
729

730
  return NULL;
×
731
}
732

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

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

747
  pStmt->bindThreadInUse = true;
×
748

749
  (void)taosThreadAttrDestroy(&thAttr);
×
750
  return TSDB_CODE_SUCCESS;
×
751
}
752

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

759
  return TSDB_CODE_SUCCESS;
×
760
}
761

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

767
  return TSDB_CODE_SUCCESS;
×
768
}
769

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

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

786
  pTblBuf->pCurBuff = buff;
×
787
  pTblBuf->buffIdx = 1;
×
788
  pTblBuf->buffOffset = 0;
×
789

790
  return TSDB_CODE_SUCCESS;
×
791
}
792

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

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

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

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

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

820
    pStmt->reqid = pOptions->reqid;
×
821
  }
822

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

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

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

869
  pStmt->execSemWaited = false;
×
870

871
  STMT_LOG_SEQ(STMT_INIT);
×
872

873
  tscDebug("stmt:%p initialized", pStmt);
×
874

875
  return pStmt;
×
876
}
877

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

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

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

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

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

902
  STMT_DLOG_E("start to prepare");
×
903

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

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

912
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
×
913

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

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

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

931
  return TSDB_CODE_SUCCESS;
×
932
}
933

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

941
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
×
942
  pStmt->sql.siInfo.pDataCtx = pDst;
×
943

944
  SArray* pTblCols = NULL;
×
945
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
×
946
    pTblCols = taosArrayInit(20, POINTER_BYTES);
×
947
    if (NULL == pTblCols) {
×
948
      return terrno;
×
949
    }
950

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

956
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
×
957

958
  return TSDB_CODE_SUCCESS;
×
959
}
960

961
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
×
962
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
963

964
  STMT_DLOG_E("start is insert");
×
965

966
  if (pStmt->sql.type) {
×
967
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
×
968
  } else {
969
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
×
970
  }
971

972
  return TSDB_CODE_SUCCESS;
×
973
}
974

975
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
×
976
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
977

978
  int64_t startUs = taosGetTimestampUs();
×
979

980
  STMT_DLOG("start to set tbName:%s", tbName);
×
981

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

986
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
×
987

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

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

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

1002
    STMT_ERR_RET(stmtGetFromCache(pStmt));
×
1003

1004
    if (pStmt->bInfo.needParse) {
×
1005
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
×
1006
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
×
1007

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

1017
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
1018
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1019
  }
1020

1021
  int64_t startUs2 = taosGetTimestampUs();
×
1022
  pStmt->stat.setTbNameUs += startUs2 - startUs;
×
1023

1024
  return TSDB_CODE_SUCCESS;
×
1025
}
1026

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

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

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

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

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

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

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

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

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

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

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

1102
  return TSDB_CODE_SUCCESS;
×
1103
}
1104

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

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

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

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

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

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

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

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

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

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

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

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

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

1176
  return TSDB_CODE_SUCCESS;
×
1177
}
1178

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

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

1189
  STableDataCxt** pDataBlock = NULL;
×
1190

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

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

1204
  return TSDB_CODE_SUCCESS;
×
1205
}
1206

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

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

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

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

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

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

1236
  STMT_ERRI_JRET(
×
1237
      qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbNameFlag, fieldNum, fields));
1238

1239
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
×
1240
    qDestroyStmtDataBlock(*pDataBlock);
×
1241
    *pDataBlock = NULL;
×
1242
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
×
1243
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
1244
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1245
    }
1246
    pStmt->sql.autoCreateTbl = false;
×
1247
    pStmt->bInfo.tagsCached = false;
×
1248
    pStmt->bInfo.sname = (SName){0};
×
1249
    stmtCleanBindInfo(pStmt);
×
1250
  }
1251

1252
_return:
×
1253

1254
  pStmt->errCode = preCode;
×
1255

1256
  return code;
×
1257
}
1258
/*
1259
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1260
  while (true) {
1261
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1262
      pStmt->exec.smInfo.pColIdx = 0;
1263
    }
1264

1265
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1266
      taosUsleep(1);
1267
      continue;
1268
    }
1269

1270
    *idx = pStmt->exec.smInfo.pColIdx;
1271
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1272
  }
1273
}
1274
*/
1275
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
×
1276
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
×
1277
    pStmt->sql.siInfo.pVgroupHash =
×
1278
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
×
1279
  }
1280
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
×
1281
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
×
1282
  }
1283

1284
  if (NULL == pStmt->sql.siInfo.pRequest) {
×
1285
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
×
1286
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1287

1288
    if (pStmt->reqid != 0) {
×
1289
      pStmt->reqid++;
×
1290
    }
1291
    pStmt->exec.pRequest->syncQuery = true;
×
1292

1293
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
×
1294
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
×
1295
  }
1296

1297
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
×
1298
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
×
1299
    pStmt->sql.siInfo.tbFromHash = true;
×
1300
  }
1301

1302
  if (0 == pStmt->sql.siInfo.firstName[0]) {
×
1303
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
×
1304
  }
1305

1306
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
×
1307
  param->next = NULL;
×
1308

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

1311
  stmtEnqueue(pStmt, param);
×
1312

1313
  return TSDB_CODE_SUCCESS;
×
1314
}
1315

1316
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1317
  while (true) {
1318
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
×
1319
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
×
1320
      break;
×
1321
    } else {
1322
      SArray* pTblCols = NULL;
×
1323
      for (int32_t i = 0; i < 100; i++) {
×
1324
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1325
        if (NULL == pTblCols) {
×
1326
          return terrno;
×
1327
        }
1328

1329
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1330
          return terrno;
×
1331
        }
1332
      }
1333
    }
1334
  }
1335

1336
  return TSDB_CODE_SUCCESS;
×
1337
}
1338

1339
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
×
1340
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
×
1341
    return TSDB_CODE_SUCCESS;
×
1342
  }
1343

1344
  uint64_t uid = pStmt->bInfo.tbUid;
×
1345
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
×
1346

1347
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
×
1348
    return TSDB_CODE_SUCCESS;
×
1349
  }
1350

1351
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1352
  if (!pSrc) {
×
1353
    return terrno;
×
1354
  }
1355
  STableDataCxt* pDst = NULL;
×
1356

1357
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
×
1358

1359
  SStmtTableCache cache = {
×
1360
      .pDataCtx = pDst,
1361
      .boundTags = pStmt->bInfo.boundTags,
×
1362
  };
1363

1364
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
×
1365
    return terrno;
×
1366
  }
1367

1368
  if (pStmt->sql.autoCreateTbl) {
×
1369
    pStmt->bInfo.tagsCached = true;
×
1370
  } else {
1371
    pStmt->bInfo.boundTags = NULL;
×
1372
  }
1373

1374
  return TSDB_CODE_SUCCESS;
×
1375
}
1376

1377
static int stmtAddBatch2(TAOS_STMT2* stmt) {
×
1378
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1379

1380
  int64_t startUs = taosGetTimestampUs();
×
1381

1382
  STMT_DLOG_E("start to add batch");
×
1383

1384
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1385
    return pStmt->errCode;
×
1386
  }
1387

1388
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
×
1389

1390
  if (pStmt->sql.stbInterlaceMode) {
×
1391
    int64_t startUs2 = taosGetTimestampUs();
×
1392
    pStmt->stat.addBatchUs += startUs2 - startUs;
×
1393

1394
    pStmt->sql.siInfo.tableColsReady = false;
×
1395

1396
    SStmtQNode* param = NULL;
×
1397
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
×
1398
    param->restoreTbCols = true;
×
1399
    param->next = NULL;
×
1400

1401
    if (pStmt->sql.autoCreateTbl) {
×
1402
      pStmt->bInfo.tagsCached = true;
×
1403
    }
1404

1405
    stmtEnqueue(pStmt, param);
×
1406

1407
    return TSDB_CODE_SUCCESS;
×
1408
  }
1409

1410
  STMT_ERR_RET(stmtCacheBlock(pStmt));
×
1411

1412
  return TSDB_CODE_SUCCESS;
×
1413
}
1414
/*
1415
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1416
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1417
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1418
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1419

1420
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1421
  pRes->fields = taosMemoryMalloc(size);
1422
  pRes->userFields = taosMemoryMalloc(size);
1423
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1424
    STMT_ERR_RET(terrno);
1425
  }
1426
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1427
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1428

1429
  return TSDB_CODE_SUCCESS;
1430
}
1431

1432
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1433
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1434
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1435

1436
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1437
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1438

1439
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1440
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1441
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1442
      STMT_ERR_RET(terrno);
1443
    }
1444
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1445
  }
1446

1447
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1448
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1449
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1450
      STMT_ERR_RET(terrno);
1451
    }
1452
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1453
  }
1454

1455
  return TSDB_CODE_SUCCESS;
1456
}
1457
*/
1458
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
×
1459
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1460
  int32_t    code = 0;
×
1461

1462
  int64_t startUs = taosGetTimestampUs();
×
1463

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

1466
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1467
    return pStmt->errCode;
×
1468
  }
1469

1470
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
×
1471

1472
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1473
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1474
    pStmt->bInfo.needParse = false;
×
1475
  }
1476

1477
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1478
    taos_free_result(pStmt->exec.pRequest);
×
1479
    pStmt->exec.pRequest = NULL;
×
1480
  }
1481

1482
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1483

1484
  if (pStmt->bInfo.needParse) {
×
1485
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1486
  }
1487

1488
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1489
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
×
1490

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

1505
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
×
1506

1507
    if (pStmt->sql.pQuery->haveResultSet) {
×
1508
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
×
1509
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1510
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
×
1511
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
×
1512
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
×
1513
    }
1514

1515
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
×
1516
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
×
1517
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
×
1518

1519
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1520
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1521
    // }
1522

1523
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1524

1525
    return TSDB_CODE_SUCCESS;
×
1526
  }
1527

1528
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
×
1529
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1530
  }
1531

1532
  STableDataCxt** pDataBlock = NULL;
×
1533

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

1555
  int64_t startUs2 = taosGetTimestampUs();
×
1556
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
×
1557

1558
  SStmtQNode* param = NULL;
×
1559
  if (pStmt->sql.stbInterlaceMode) {
×
1560
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
×
1561
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
×
1562
    taosArrayClear(param->tblData.aCol);
×
1563

1564
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1565

1566
    param->restoreTbCols = false;
×
1567
    param->tblData.isOrdered = true;
×
1568
    param->tblData.isDuplicateTs = false;
×
1569
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
×
1570

1571
    param->pCreateTbReq = pCreateTbReq;
×
1572
  }
1573

1574
  int64_t startUs3 = taosGetTimestampUs();
×
1575
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
×
1576

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

1579
  if (colIdx < 0) {
×
1580
    if (pStmt->sql.stbInterlaceMode) {
×
1581
      // (*pDataBlock)->pData->flags = 0;
1582
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
×
1583
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1584
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
×
1585
                                    pStmt->taos->optionInfo.charsetCxt);
×
1586
      param->tblData.isOrdered = (*pDataBlock)->ordered;
×
1587
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
×
1588
    } else {
1589
      if (colIdx == -1) {
×
1590
        if (pStmt->sql.bindRowFormat) {
×
1591
          tscError("can't mix bind row format and bind column format");
×
1592
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1593
        }
1594
        code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1595
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
×
1596
      } else {
1597
        code = qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, bind, pStmt->exec.pRequest->msgBuf,
×
1598
                                  pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
×
1599
                                  pStmt->taos->optionInfo.charsetCxt);
×
1600
      }
1601
    }
1602

1603
    if (code) {
×
1604
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
×
1605
      STMT_ERR_RET(code);
×
1606
    }
1607
  } else {
1608
    if (pStmt->sql.stbInterlaceMode) {
×
1609
      tscError("bind single column not allowed in stb insert mode");
×
1610
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1611
    }
1612

1613
    if (pStmt->sql.bindRowFormat) {
×
1614
      tscError("can't mix bind row format and bind column format");
×
1615
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1616
    }
1617

1618
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1619
      tscError("bind column index not in sequence");
×
1620
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1621
    }
1622

1623
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1624

1625
    if (0 == colIdx) {
×
1626
      pStmt->bInfo.sBindRowNum = bind->num;
×
1627
    }
1628

1629
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
1630
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1631
    if (code) {
×
1632
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1633
      STMT_ERR_RET(code);
×
1634
    }
1635
  }
1636

1637
  int64_t startUs4 = taosGetTimestampUs();
×
1638
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
×
1639

1640
  if (pStmt->sql.stbInterlaceMode) {
×
1641
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
×
1642
  } else {
1643
    STMT_ERR_RET(stmtAddBatch2(pStmt));
×
1644
  }
1645

1646
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
×
1647

1648
  return TSDB_CODE_SUCCESS;
×
1649
}
1650
/*
1651
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1652
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1653

1654
  int32_t code = 0;
1655
  int32_t finalCode = 0;
1656
  size_t  keyLen = 0;
1657
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1658
  while (pIter) {
1659
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1660
    char*          key = taosHashGetKey(pIter, &keyLen);
1661

1662
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1663
    if (pMeta->uid) {
1664
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1665
      continue;
1666
    }
1667

1668
    SSubmitBlkRsp* blkRsp = NULL;
1669
    int32_t        i = 0;
1670
    for (; i < pRsp->nBlocks; ++i) {
1671
      blkRsp = pRsp->pBlocks + i;
1672
      if (strlen(blkRsp->tblFName) != keyLen) {
1673
        continue;
1674
      }
1675

1676
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1677
        continue;
1678
      }
1679

1680
      break;
1681
    }
1682

1683
    if (i < pRsp->nBlocks) {
1684
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1685
               blkRsp->uid);
1686

1687
      pMeta->uid = blkRsp->uid;
1688
      pStmt->bInfo.tbUid = blkRsp->uid;
1689
    } else {
1690
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1691
      if (NULL == pStmt->pCatalog) {
1692
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1693
        if (code) {
1694
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1695
          finalCode = code;
1696
          continue;
1697
        }
1698
      }
1699

1700
      code = stmtCreateRequest(pStmt);
1701
      if (code) {
1702
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1703
        finalCode = code;
1704
        continue;
1705
      }
1706

1707
      STableMeta*      pTableMeta = NULL;
1708
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1709
                               .requestId = pStmt->exec.pRequest->requestId,
1710
                               .requestObjRefId = pStmt->exec.pRequest->self,
1711
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1712
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1713

1714
      pStmt->stat.ctgGetTbMetaNum++;
1715

1716
      taos_free_result(pStmt->exec.pRequest);
1717
      pStmt->exec.pRequest = NULL;
1718

1719
      if (code || NULL == pTableMeta) {
1720
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1721
        finalCode = code;
1722
        taosMemoryFree(pTableMeta);
1723
        continue;
1724
      }
1725

1726
      pMeta->uid = pTableMeta->uid;
1727
      pStmt->bInfo.tbUid = pTableMeta->uid;
1728
      taosMemoryFree(pTableMeta);
1729
    }
1730

1731
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1732
  }
1733

1734
  return finalCode;
1735
}
1736
*/
1737
/*
1738
int stmtStaticModeExec(TAOS_STMT* stmt) {
1739
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1740
  int32_t     code = 0;
1741
  SSubmitRsp* pRsp = NULL;
1742
  if (pStmt->sql.staticMode) {
1743
    return TSDB_CODE_TSC_STMT_API_ERROR;
1744
  }
1745

1746
  STMT_DLOG_E("start to exec");
1747

1748
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1749

1750
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1751
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1752

1753
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1754

1755
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1756
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1757
    if (code) {
1758
      pStmt->exec.pRequest->code = code;
1759
    } else {
1760
      tFreeSSubmitRsp(pRsp);
1761
      STMT_ERR_RET(stmtResetStmt(pStmt));
1762
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1763
    }
1764
  }
1765

1766
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1767

1768
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1769
  pStmt->affectedRows += pStmt->exec.affectedRows;
1770

1771
_return:
1772

1773
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1774

1775
  tFreeSSubmitRsp(pRsp);
1776

1777
  ++pStmt->sql.runTimes;
1778

1779
  STMT_RET(code);
1780
}
1781
*/
1782

1783
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
1784
  const STscObj* pTscObj = pRequest->pTscObj;
×
1785

1786
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
1787
  if (*pCxt == NULL) {
×
1788
    return terrno;
×
1789
  }
1790

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

1817
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
1818
  STscStmt2*        pStmt = userdata;
×
1819
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1820

1821
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1822
  pStmt->affectedRows += pStmt->exec.affectedRows;
×
1823

1824
  fp(pStmt->options.userdata, res, code);
×
1825

1826
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1827
    taosUsleep(1);
×
1828
  }
1829
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
1830
  ++pStmt->sql.runTimes;
×
1831

1832
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
1833
    tscError("failed to post asyncExecSem");
×
1834
  }
1835
}
×
1836

1837
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
×
1838
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1839
  int32_t    code = 0;
×
1840
  int64_t    startUs = taosGetTimestampUs();
×
1841

1842
  STMT_DLOG_E("start to exec");
×
1843

1844
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1845
    return pStmt->errCode;
×
1846
  }
1847

1848
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
×
1849
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
1850
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1851
  }
1852
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
×
1853

1854
  if (pStmt->sql.stbInterlaceMode) {
×
1855
    STMT_ERR_RET(stmtAddBatch2(pStmt));
×
1856
  }
1857

1858
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
×
1859

1860
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
1861
    if (pStmt->sql.stbInterlaceMode) {
×
1862
      int64_t startTs = taosGetTimestampUs();
×
1863
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
×
1864
        taosUsleep(1);
×
1865
      }
1866
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
×
1867

1868
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
×
1869
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
1870
      pStmt->sql.siInfo.pVgroupHash = NULL;
×
1871
      pStmt->sql.siInfo.pVgroupList = NULL;
×
1872
    } else {
1873
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
×
1874
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
×
1875

1876
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
×
1877

1878
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
×
1879
    }
1880
  }
1881

1882
  SRequestObj*      pRequest = pStmt->exec.pRequest;
×
1883
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
1884

1885
  if (!fp) {
×
1886
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
×
1887

1888
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
×
1889
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1890
      if (code) {
×
1891
        pStmt->exec.pRequest->code = code;
×
1892
      } else {
1893
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1894
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1895
      }
1896
    }
1897

1898
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
×
1899

1900
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
×
1901
    if (affected_rows) {
×
1902
      *affected_rows = pStmt->exec.affectedRows;
×
1903
    }
1904
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
1905

1906
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
1907
      taosUsleep(1);
×
1908
    }
1909

1910
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
×
1911

1912
    ++pStmt->sql.runTimes;
×
1913
  } else {
1914
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
1915
    if (pWrapper == NULL) {
×
1916
      code = terrno;
×
1917
    } else {
1918
      pWrapper->pRequest = pRequest;
×
1919
      pRequest->pWrapper = pWrapper;
×
1920
    }
1921
    if (TSDB_CODE_SUCCESS == code) {
×
1922
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
1923
    }
1924
    pRequest->syncQuery = false;
×
1925
    pRequest->body.queryFp = asyncQueryCb;
×
1926
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
1927

1928
    pStmt->execSemWaited = false;
×
1929
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
1930
  }
1931

1932
_return:
×
1933
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
×
1934

1935
  STMT_RET(code);
×
1936
}
1937

1938
int stmtClose2(TAOS_STMT2* stmt) {
×
1939
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1940

1941
  STMT_DLOG_E("start to free stmt");
×
1942

1943
  if (pStmt->bindThreadInUse) {
×
1944
    pStmt->queue.stopQueue = true;
×
1945

1946
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
×
1947
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
×
1948
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
×
1949
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
1950

1951
    (void)taosThreadJoin(pStmt->bindThread, NULL);
×
1952
    pStmt->bindThreadInUse = false;
×
1953

1954
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
×
1955
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
×
1956
  }
1957

1958
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
×
1959
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
×
1960
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1961
  }
1962
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
×
1963

1964
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
×
1965
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
×
1966

1967
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
×
1968
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
1969
      tscError("failed to wait asyncExecSem");
×
1970
    }
1971
  }
1972

1973
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
×
1974
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1975
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1976
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1977
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1978
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1979
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1980
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1981
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1982
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1983

1984
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
1985

1986
  if (pStmt->options.asyncExecFn) {
×
1987
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
1988
      tscError("failed to destroy asyncExecSem");
×
1989
    }
1990
  }
1991
  taosMemoryFree(stmt);
×
1992

1993
  return TSDB_CODE_SUCCESS;
×
1994
}
1995

1996
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
1997
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1998

1999
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
2000
    return (char*)tstrerror(terrno);
×
2001
  }
2002

2003
  pStmt->exec.pRequest->code = terrno;
×
2004

2005
  return taos_errstr(pStmt->exec.pRequest);
×
2006
}
2007
/*
2008
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2009

2010
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2011
*/
2012

2013
int stmtParseColFields2(TAOS_STMT2* stmt) {
×
2014
  int32_t    code = 0;
×
2015
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2016
  int32_t    preCode = pStmt->errCode;
×
2017

2018
  STMT_DLOG_E("start to get col fields");
×
2019

2020
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2021
    return pStmt->errCode;
×
2022
  }
2023

2024
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2025
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2026
  }
2027

2028
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2029

2030
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2031
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2032
    pStmt->bInfo.needParse = false;
×
2033
  }
2034
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
×
2035
    pStmt->bInfo.needParse = false;
×
2036
  }
2037
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2038
    taos_free_result(pStmt->exec.pRequest);
×
2039
    pStmt->exec.pRequest = NULL;
×
2040
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2041
  }
2042

2043
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2044

2045
  if (pStmt->bInfo.needParse) {
×
2046
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2047
  }
2048

2049
_return:
×
2050
  // compatible with previous versions
2051
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
×
2052
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2053
  }
2054

2055
  pStmt->errCode = preCode;
×
2056

2057
  return code;
×
2058
}
2059

2060
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
×
2061
  int32_t code = stmtParseColFields2(stmt);
×
2062
  if (code != TSDB_CODE_SUCCESS) {
×
2063
    return code;
×
2064
  }
2065

2066
  return stmtFetchStbColFields2(stmt, nums, fields);
×
2067
}
2068

2069
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2070
  int32_t    code = 0;
×
2071
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2072
  int32_t    preCode = pStmt->errCode;
×
2073

2074
  STMT_DLOG_E("start to get param num");
×
2075

2076
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2077
    return pStmt->errCode;
×
2078
  }
2079

2080
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2081

2082
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2083
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2084
    pStmt->bInfo.needParse = false;
×
2085
  }
2086

2087
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2088
    taos_free_result(pStmt->exec.pRequest);
×
2089
    pStmt->exec.pRequest = NULL;
×
2090
  }
2091

2092
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2093

2094
  if (pStmt->bInfo.needParse) {
×
2095
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2096
  }
2097

2098
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2099
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2100
  } else {
2101
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2102
  }
2103

2104
_return:
×
2105

2106
  pStmt->errCode = preCode;
×
2107

2108
  return code;
×
2109
}
2110

2111
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
×
2112
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2113

2114
  STMT_DLOG_E("start to use result");
×
2115

2116
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
×
2117
    tscError("useResult only for query statement");
×
2118
    return NULL;
×
2119
  }
2120

2121
  return pStmt->exec.pRequest;
×
2122
}
2123

2124
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2125
  qInfo("async stmt bind thread started");
×
2126

2127
  ThreadArgs* targs = (ThreadArgs*)args;
×
2128
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2129

2130
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2131
  targs->fp(targs->param, NULL, code);
×
2132
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2133
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2134
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2135
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2136
  taosMemoryFree(args);
×
2137

2138
  qInfo("async stmt bind thread stopped");
×
2139

2140
  return code;
×
2141
}
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