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

taosdata / TDengine / #3846

11 Apr 2025 06:14AM UTC coverage: 62.398% (-0.2%) from 62.585%
#3846

push

travis-ci

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

merge: from main to 3.0 branch

153660 of 315259 branches covered (48.74%)

Branch coverage included in aggregate %.

63 of 80 new or added lines in 9 files covered. (78.75%)

1385 existing lines in 115 files now uncovered.

239696 of 315138 relevant lines covered (76.06%)

18295808.9 hits per line

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

65.96
/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) {
10,222✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
10,226✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
10,226✔
UNCOV
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;
10,226✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
10,231✔
42
  int i = 0;
10,231✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
56,714✔
44
    if (i < 10) {
46,466✔
45
      taosUsleep(1);
43,831✔
46
      i++;
43,849✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,635✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
2,636✔
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
2,626✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,632✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
10,064✔
56
    return false;
78✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
9,986✔
59
  SStmtQNode* node = pStmt->queue.head->next;
9,986✔
60
  pStmt->queue.head = pStmt->queue.head->next;
9,986✔
61
  *param = node;
9,986✔
62

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

65
  return true;
10,161✔
66
}
67

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

72
  pStmt->stat.bindDataNum++;
10,146✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
10,146✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
10,159✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
10,161✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
10,157✔
78
}
10,153✔
79

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

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

100
  return code;
6,390✔
101
}
102

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

106
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
20,956!
107
    STMT_LOG_SEQ(newStatus);
20,967!
108
  }
109

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

115
  switch (newStatus) {
20,986!
116
    case STMT_PREPARE:
142✔
117
      pStmt->errCode = 0;
142✔
118
      break;
142✔
119
    case STMT_SETTBNAME:
5,846✔
120
      if (STMT_STATUS_EQ(INIT)) {
5,846!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
5,846!
124
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
125
      }
126
      break;
5,846✔
127
    case STMT_SETTAGS:
124✔
128
      if (STMT_STATUS_EQ(INIT)) {
124!
129
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
130
      }
131
      break;
124✔
132
    case STMT_FETCH_FIELDS:
56✔
133
      if (STMT_STATUS_EQ(INIT)) {
56!
134
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
135
      }
136
      break;
56✔
137
    case STMT_BIND:
5,843✔
138
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
5,843!
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;
5,843✔
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:
4,517✔
153
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
4,517!
154
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
155
      }
156
      break;
4,517✔
157
    case STMT_EXECUTE:
4,458✔
158
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
4,458✔
159
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
3!
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)) {
4,455!
165
          code = TSDB_CODE_TSC_STMT_API_ERROR;
1✔
166
        }
167
      }
168
      break;
4,458✔
169
    default:
×
170
      code = TSDB_CODE_APP_ERROR;
×
171
      break;
×
172
  }
173

174
  STMT_ERR_RET(code);
20,986✔
175

176
  pStmt->sql.status = newStatus;
20,985✔
177

178
  return TSDB_CODE_SUCCESS;
20,985✔
179
}
180

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

184
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
109✔
185

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

191
  *tbName = pStmt->bInfo.tbName;
86✔
192

193
  return TSDB_CODE_SUCCESS;
86✔
194
}
195

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

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

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

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

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

224
  return TSDB_CODE_SUCCESS;
110✔
225
}
226

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

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

233
  return TSDB_CODE_SUCCESS;
108✔
234
}
235

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

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

243
  pStmt->sql.autoCreateTbl = autoCreateTbl;
108✔
244

245
  return TSDB_CODE_SUCCESS;
108✔
246
}
247

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

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

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

257
  return TSDB_CODE_SUCCESS;
1✔
258
}
259

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

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

270
  STMT_ERR_RET(stmtCreateRequest(pStmt));
141!
271

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

276
  pStmt->bInfo.needParse = false;
119✔
277

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

285
    return TSDB_CODE_SUCCESS;
4✔
286
  }
287

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

294
  STableDataCxt* pTableCtx = *pSrc;
115✔
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) {
115✔
309
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
114!
310
    if (NULL == pStmt->sql.pBindInfo) {
114!
311
      return terrno;
×
312
    }
313
  }
314

315
  return TSDB_CODE_SUCCESS;
115✔
316
}
317

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

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

336
  return TSDB_CODE_SUCCESS;
4,707✔
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) {
4,404✔
345
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
4,404✔
346
  if (NULL == pTblBuf->pCurBuff) {
4,407✔
347
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
3!
348
    return;
×
349
  }
350
  pTblBuf->buffIdx = 1;
4,404✔
351
  pTblBuf->buffOffset = sizeof(*pQueue->head);
4,404✔
352

353
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
4,404✔
354
  pQueue->qRemainNum = 0;
4,404✔
355
  pQueue->head->next = NULL;
4,404✔
356
}
357

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

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

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

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

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

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

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

404
    if (keepTable) {
111✔
405
      return TSDB_CODE_SUCCESS;
44✔
406
    }
407

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

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

415
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
4,550!
416

417
  return TSDB_CODE_SUCCESS;
4,546✔
418
}
419

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

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

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

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

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

450
    qDestroyStmtDataBlock(pCache->pDataCtx);
14✔
451
    qDestroyBoundColInfo(pCache->boundTags);
14✔
452
    taosMemoryFreeClear(pCache->boundTags);
14!
453

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

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

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

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

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

475
  return TSDB_CODE_SUCCESS;
143✔
476
}
477

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

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

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

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

500
  *vgId = vgInfo.vgId;
94✔
501

502
  return TSDB_CODE_SUCCESS;
94✔
503
}
504

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

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

512
  return TSDB_CODE_SUCCESS;
63✔
513
}
514

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

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

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

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

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

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

543
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
156!
544
    if (pStmt->bInfo.inExecCache) {
86!
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);
86!
551
    return TSDB_CODE_SUCCESS;
86✔
552
  }
553

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

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

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

568
      pStmt->exec.pCurrBlock = pNewBlock;
45✔
569

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

572
      return TSDB_CODE_SUCCESS;
45✔
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;
24✔
583
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
24✔
584
                           .requestId = pStmt->exec.pRequest->requestId,
24✔
585
                           .requestObjRefId = pStmt->exec.pRequest->self,
24✔
586
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
24✔
587
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
24✔
588

589
  pStmt->stat.ctgGetTbMetaNum++;
24✔
590

591
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
24!
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);
24!
599

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

606
  taosMemoryFree(pTableMeta);
24!
607

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

610
  if (uid == pStmt->bInfo.tbUid) {
24!
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) {
24✔
619
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
6✔
620
    if (NULL == pCache) {
6!
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;
6✔
628

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

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

637
    return TSDB_CODE_SUCCESS;
6✔
638
  }
639

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

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

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

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

658
    pStmt->exec.pCurrBlock = pNewBlock;
18✔
659

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

662
    return TSDB_CODE_SUCCESS;
18✔
663
  }
664

665
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
666

667
  return TSDB_CODE_SUCCESS;
×
668
}
669

670
static int32_t stmtResetStmt(STscStmt2* pStmt) {
1✔
671
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
1!
672

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

678
  pStmt->sql.status = STMT_INIT;
1✔
679

680
  return TSDB_CODE_SUCCESS;
1✔
681
}
682

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

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

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

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

709
  qInfo("stmt bind thread started");
80!
710

711
  STscStmt2* pStmt = (STscStmt2*)param;
80✔
712

713
  while (true) {
10,233✔
714
    if (pStmt->queue.stopQueue) {
10,313✔
715
      break;
80✔
716
    }
717

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

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

728
  qInfo("stmt bind thread stopped");
80!
729

730
  return NULL;
80✔
731
}
732

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

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

747
  pStmt->bindThreadInUse = true;
80✔
748

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

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

759
  return TSDB_CODE_SUCCESS;
80✔
760
}
761

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

767
  return TSDB_CODE_SUCCESS;
144✔
768
}
769

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

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

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

790
  return TSDB_CODE_SUCCESS;
80✔
791
}
792

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

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

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

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

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

820
    pStmt->reqid = pOptions->reqid;
144✔
821
  }
822

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

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

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

869
  pStmt->execSemWaited = false;
144✔
870

871
  STMT_LOG_SEQ(STMT_INIT);
144!
872

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

875
  return pStmt;
144✔
876
}
877

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

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

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

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

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

902
  STMT_DLOG_E("start to prepare");
143!
903

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

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

912
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
142!
913

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

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

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

931
  return TSDB_CODE_SUCCESS;
142✔
932
}
933

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

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

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

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

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

958
  return TSDB_CODE_SUCCESS;
13✔
959
}
960

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

964
  STMT_DLOG_E("start is insert");
11,738!
965

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

972
  return TSDB_CODE_SUCCESS;
11,742✔
973
}
974

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

978
  int64_t startUs = taosGetTimestampUs();
5,842✔
979

980
  STMT_DLOG("start to set tbName:%s", tbName);
5,842!
981

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

986
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
5,843!
987

988
  int32_t insert = 0;
5,846✔
989
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
5,846!
990
  if (0 == insert) {
5,844!
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) {
5,844✔
996
    STMT_ERR_RET(stmtCreateRequest(pStmt));
176!
997

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

1002
    STMT_ERR_RET(stmtGetFromCache(pStmt));
173!
1003

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

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

1017
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
5,843✔
1018
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
73!
1019
  }
1020

1021
  int64_t startUs2 = taosGetTimestampUs();
5,842✔
1022
  pStmt->stat.setTbNameUs += startUs2 - startUs;
5,842✔
1023

1024
  return TSDB_CODE_SUCCESS;
5,842✔
1025
}
1026

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

1030
  STMT_DLOG_E("start to set tbTags");
112!
1031

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

1036
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
112!
1037

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

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

1051
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
112✔
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) {
112!
1057
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, pStmt->bInfo.tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
37!
1058
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1059
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
37!
1060
  }
1061

1062
  STableDataCxt** pDataBlock = NULL;
112✔
1063
  if (pStmt->exec.pCurrBlock) {
112✔
1064
    pDataBlock = &pStmt->exec.pCurrBlock;
94✔
1065
  } else {
1066
    pDataBlock =
1067
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
18✔
1068
    if (NULL == pDataBlock) {
18!
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) {
112!
1079
    return TSDB_CODE_SUCCESS;
×
1080
  }
1081

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

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

1098
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
112✔
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;
111✔
1103
}
1104

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

1108
  STMT_DLOG_E("start to set tbTags");
12!
1109

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

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

1118
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
12!
1119

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

1126
  if (pStmt->bInfo.needParse) {
12!
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) {
12!
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,
12!
1138
                            pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1139
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
12!
1140

1141
  STableDataCxt** pDataBlock = NULL;
12✔
1142
  if (pStmt->exec.pCurrBlock) {
12✔
1143
    pDataBlock = &pStmt->exec.pCurrBlock;
9✔
1144
  } else {
1145
    pDataBlock =
1146
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3✔
1147
    if (NULL == pDataBlock) {
3!
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)) {
12!
1154
    return TSDB_CODE_SUCCESS;
×
1155
  }
1156

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

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

1176
  return TSDB_CODE_SUCCESS;
3✔
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) {
32✔
1208
  int32_t    code = 0;
32✔
1209
  int32_t    preCode = pStmt->errCode;
32✔
1210

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

1215
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
32!
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;
32✔
1221
  bool            cleanStb = false;
32✔
1222

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

1231
  if (NULL == pDataBlock || NULL == *pDataBlock) {
32!
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(
32!
1237
      qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbNameFlag, fieldNum, fields));
1238

1239
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
32!
1240
    qDestroyStmtDataBlock(*pDataBlock);
18✔
1241
    *pDataBlock = NULL;
18✔
1242
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
18!
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;
18✔
1247
    pStmt->bInfo.tagsCached = false;
18✔
1248
    pStmt->bInfo.sname = (SName){0};
18✔
1249
    stmtCleanBindInfo(pStmt);
18✔
1250
  }
1251

1252
_return:
14✔
1253

1254
  pStmt->errCode = preCode;
32✔
1255

1256
  return code;
32✔
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) {
5,738✔
1276
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,738✔
1277
    pStmt->sql.siInfo.pVgroupHash =
4,405✔
1278
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,400✔
1279
  }
1280
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,743✔
1281
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,405✔
1282
  }
1283

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

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

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

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

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

1306
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,744✔
1307
  param->next = NULL;
5,744✔
1308

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

1311
  stmtEnqueue(pStmt, param);
5,749✔
1312

1313
  return TSDB_CODE_SUCCESS;
5,745✔
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)) {
5,740!
1319
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,740✔
1320
      break;
5,741✔
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;
5,741✔
1337
}
1338

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

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

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

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

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

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

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

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

1374
  return TSDB_CODE_SUCCESS;
14✔
1375
}
1376

1377
static int stmtAddBatch2(TAOS_STMT2* stmt) {
4,517✔
1378
  STscStmt2* pStmt = (STscStmt2*)stmt;
4,517✔
1379

1380
  int64_t startUs = taosGetTimestampUs();
4,518✔
1381

1382
  STMT_DLOG_E("start to add batch");
4,518!
1383

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

1388
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
4,517!
1389

1390
  if (pStmt->sql.stbInterlaceMode) {
4,517✔
1391
    int64_t startUs2 = taosGetTimestampUs();
4,406✔
1392
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,406✔
1393

1394
    pStmt->sql.siInfo.tableColsReady = false;
4,406✔
1395

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

1401
    if (pStmt->sql.autoCreateTbl) {
4,406✔
1402
      pStmt->bInfo.tagsCached = true;
21✔
1403
    }
1404

1405
    stmtEnqueue(pStmt, param);
4,406✔
1406

1407
    return TSDB_CODE_SUCCESS;
4,411✔
1408
  }
1409

1410
  STMT_ERR_RET(stmtCacheBlock(pStmt));
107!
1411

1412
  return TSDB_CODE_SUCCESS;
107✔
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) {
5,837✔
1459
  STscStmt2* pStmt = (STscStmt2*)stmt;
5,837✔
1460
  int32_t    code = 0;
5,837✔
1461

1462
  int64_t startUs = taosGetTimestampUs();
5,842✔
1463

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

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

1470
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
5,843!
1471

1472
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
5,843!
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) {
5,843!
1478
    taos_free_result(pStmt->exec.pRequest);
1✔
1479
    pStmt->exec.pRequest = NULL;
1✔
1480
  }
1481

1482
  STMT_ERR_RET(stmtCreateRequest(pStmt));
5,843!
1483
  if (pStmt->bInfo.needParse) {
5,842✔
1484
    code = stmtParseSql(pStmt);
4✔
1485
    if (code != TSDB_CODE_SUCCESS) {
4!
1486
      goto cleanup_root;
×
1487
    }
1488
  }
1489

1490
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
5,842✔
1491
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
3✔
1492
    if (code != TSDB_CODE_SUCCESS) {
3!
1493
      goto cleanup_root;
×
1494
    }
1495
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
3✔
1496
                         .acctId = pStmt->taos->acctId,
3✔
1497
                         .db = pStmt->exec.pRequest->pDb,
3✔
1498
                         .topicQuery = false,
1499
                         .pSql = pStmt->sql.sqlStr,
3✔
1500
                         .sqlLen = pStmt->sql.sqlLen,
3✔
1501
                         .pMsg = pStmt->exec.pRequest->msgBuf,
3✔
1502
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1503
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
3✔
1504
                         .pStmtCb = NULL,
1505
                         .pUser = pStmt->taos->user};
3✔
1506
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
3✔
1507
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
3✔
1508
    if (code != TSDB_CODE_SUCCESS) {
3!
1509
      goto cleanup_root;
×
1510
    }
1511
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
3✔
1512
    if (code != TSDB_CODE_SUCCESS) {
3!
1513
      goto cleanup_root;
×
1514
    }
1515

1516
    if (pStmt->sql.pQuery->haveResultSet) {
3!
1517
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
6!
1518
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1519
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
3!
1520
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
3!
1521
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
3✔
1522
    }
1523

1524
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
3✔
1525
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
3✔
1526
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
3✔
1527

1528
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1529
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1530
    // }
1531

1532
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1533

1534
    return TSDB_CODE_SUCCESS;
3✔
1535

1536
  cleanup_root:
×
1537
    if (pStmt->sql.pQuery->pRoot) {
×
1538
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
×
1539
      pStmt->sql.pQuery->pRoot = NULL;
×
1540
    }
1541
    STMT_ERR_RET(code);
×
1542
  }
1543

1544
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
5,839!
1545
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1546
  }
1547

1548
  STableDataCxt** pDataBlock = NULL;
5,843✔
1549

1550
  if (pStmt->exec.pCurrBlock) {
5,843✔
1551
    pDataBlock = &pStmt->exec.pCurrBlock;
5,755✔
1552
  } else {
1553
    pDataBlock =
1554
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
88✔
1555
    if (NULL == pDataBlock) {
88!
1556
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1557
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1558
    }
1559
    pStmt->exec.pCurrBlock = *pDataBlock;
88✔
1560
    if (pStmt->sql.stbInterlaceMode) {
88✔
1561
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
72✔
1562
      (*pDataBlock)->pData->aCol = NULL;
72✔
1563
    }
1564
    if (colIdx < -1) {
88✔
1565
      pStmt->sql.bindRowFormat = true;
1✔
1566
      taosArrayDestroy((*pDataBlock)->pData->aCol);
1✔
1567
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
1✔
1568
    }
1569
  }
1570

1571
  int64_t startUs2 = taosGetTimestampUs();
5,840✔
1572
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
5,840✔
1573

1574
  SStmtQNode* param = NULL;
5,840✔
1575
  if (pStmt->sql.stbInterlaceMode) {
5,840✔
1576
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,476!
1577
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,481!
1578
    taosArrayClear(param->tblData.aCol);
5,741✔
1579

1580
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1581

1582
    param->restoreTbCols = false;
5,737✔
1583
    param->tblData.isOrdered = true;
5,737✔
1584
    param->tblData.isDuplicateTs = false;
5,737✔
1585
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,737✔
1586

1587
    param->pCreateTbReq = pCreateTbReq;
5,737✔
1588
  }
1589

1590
  int64_t startUs3 = taosGetTimestampUs();
5,843✔
1591
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
5,843✔
1592

1593
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
5,843✔
1594

1595
  if (colIdx < 0) {
5,843✔
1596
    if (pStmt->sql.stbInterlaceMode) {
5,840✔
1597
      // (*pDataBlock)->pData->flags = 0;
1598
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
5,739✔
1599
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
5,739✔
1600
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
5,739✔
1601
                                    pStmt->taos->optionInfo.charsetCxt);
5,739✔
1602
      param->tblData.isOrdered = (*pDataBlock)->ordered;
5,741✔
1603
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
5,741✔
1604
    } else {
1605
      if (colIdx == -1) {
102✔
1606
        if (pStmt->sql.bindRowFormat) {
100✔
1607
          tscError("can't mix bind row format and bind column format");
1!
1608
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
1!
1609
        }
1610
        code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
99✔
1611
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
99✔
1612
      } else {
1613
        code = qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, bind, pStmt->exec.pRequest->msgBuf,
2✔
1614
                                  pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
2✔
1615
                                  pStmt->taos->optionInfo.charsetCxt);
2✔
1616
      }
1617
    }
1618

1619
    if (code) {
5,842✔
1620
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1621
      STMT_ERR_RET(code);
1!
1622
    }
1623
  } else {
1624
    if (pStmt->sql.stbInterlaceMode) {
6!
1625
      tscError("bind single column not allowed in stb insert mode");
×
1626
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1627
    }
1628

1629
    if (pStmt->sql.bindRowFormat) {
6!
1630
      tscError("can't mix bind row format and bind column format");
×
1631
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1632
    }
1633

1634
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
6!
1635
      tscError("bind column index not in sequence");
×
1636
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1637
    }
1638

1639
    pStmt->bInfo.sBindLastIdx = colIdx;
6✔
1640

1641
    if (0 == colIdx) {
6✔
1642
      pStmt->bInfo.sBindRowNum = bind->num;
3✔
1643
    }
1644

1645
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
6✔
1646
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
6✔
1647
    if (code) {
6!
1648
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1649
      STMT_ERR_RET(code);
×
1650
    }
1651
  }
1652

1653
  int64_t startUs4 = taosGetTimestampUs();
5,844✔
1654
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
5,844✔
1655

1656
  if (pStmt->sql.stbInterlaceMode) {
5,844✔
1657
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,738!
1658
  } else {
1659
    STMT_ERR_RET(stmtAddBatch2(pStmt));
107!
1660
  }
1661

1662
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
5,854✔
1663

1664
  return TSDB_CODE_SUCCESS;
5,854✔
1665
}
1666
/*
1667
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1668
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1669

1670
  int32_t code = 0;
1671
  int32_t finalCode = 0;
1672
  size_t  keyLen = 0;
1673
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1674
  while (pIter) {
1675
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1676
    char*          key = taosHashGetKey(pIter, &keyLen);
1677

1678
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1679
    if (pMeta->uid) {
1680
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1681
      continue;
1682
    }
1683

1684
    SSubmitBlkRsp* blkRsp = NULL;
1685
    int32_t        i = 0;
1686
    for (; i < pRsp->nBlocks; ++i) {
1687
      blkRsp = pRsp->pBlocks + i;
1688
      if (strlen(blkRsp->tblFName) != keyLen) {
1689
        continue;
1690
      }
1691

1692
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1693
        continue;
1694
      }
1695

1696
      break;
1697
    }
1698

1699
    if (i < pRsp->nBlocks) {
1700
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1701
               blkRsp->uid);
1702

1703
      pMeta->uid = blkRsp->uid;
1704
      pStmt->bInfo.tbUid = blkRsp->uid;
1705
    } else {
1706
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1707
      if (NULL == pStmt->pCatalog) {
1708
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1709
        if (code) {
1710
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1711
          finalCode = code;
1712
          continue;
1713
        }
1714
      }
1715

1716
      code = stmtCreateRequest(pStmt);
1717
      if (code) {
1718
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1719
        finalCode = code;
1720
        continue;
1721
      }
1722

1723
      STableMeta*      pTableMeta = NULL;
1724
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1725
                               .requestId = pStmt->exec.pRequest->requestId,
1726
                               .requestObjRefId = pStmt->exec.pRequest->self,
1727
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1728
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1729

1730
      pStmt->stat.ctgGetTbMetaNum++;
1731

1732
      taos_free_result(pStmt->exec.pRequest);
1733
      pStmt->exec.pRequest = NULL;
1734

1735
      if (code || NULL == pTableMeta) {
1736
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1737
        finalCode = code;
1738
        taosMemoryFree(pTableMeta);
1739
        continue;
1740
      }
1741

1742
      pMeta->uid = pTableMeta->uid;
1743
      pStmt->bInfo.tbUid = pTableMeta->uid;
1744
      taosMemoryFree(pTableMeta);
1745
    }
1746

1747
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1748
  }
1749

1750
  return finalCode;
1751
}
1752
*/
1753
/*
1754
int stmtStaticModeExec(TAOS_STMT* stmt) {
1755
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1756
  int32_t     code = 0;
1757
  SSubmitRsp* pRsp = NULL;
1758
  if (pStmt->sql.staticMode) {
1759
    return TSDB_CODE_TSC_STMT_API_ERROR;
1760
  }
1761

1762
  STMT_DLOG_E("start to exec");
1763

1764
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1765

1766
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1767
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1768

1769
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1770

1771
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1772
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1773
    if (code) {
1774
      pStmt->exec.pRequest->code = code;
1775
    } else {
1776
      tFreeSSubmitRsp(pRsp);
1777
      STMT_ERR_RET(stmtResetStmt(pStmt));
1778
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1779
    }
1780
  }
1781

1782
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1783

1784
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1785
  pStmt->affectedRows += pStmt->exec.affectedRows;
1786

1787
_return:
1788

1789
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1790

1791
  tFreeSSubmitRsp(pRsp);
1792

1793
  ++pStmt->sql.runTimes;
1794

1795
  STMT_RET(code);
1796
}
1797
*/
1798

1799
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
12✔
1800
  const STscObj* pTscObj = pRequest->pTscObj;
12✔
1801

1802
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
12!
1803
  if (*pCxt == NULL) {
12!
1804
    return terrno;
×
1805
  }
1806

1807
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
12✔
1808
                           .requestRid = pRequest->self,
12✔
1809
                           .acctId = pTscObj->acctId,
12✔
1810
                           .db = pRequest->pDb,
12✔
1811
                           .topicQuery = false,
1812
                           .pSql = pRequest->sqlstr,
12✔
1813
                           .sqlLen = pRequest->sqlLen,
12✔
1814
                           .pMsg = pRequest->msgBuf,
12✔
1815
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1816
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
12✔
1817
                           .pStmtCb = NULL,
1818
                           .pUser = pTscObj->user,
12✔
1819
                           .pEffectiveUser = pRequest->effectiveUser,
12✔
1820
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
12✔
1821
                           .enableSysInfo = pTscObj->sysInfo,
12✔
1822
                           .async = true,
1823
                           .svrVer = pTscObj->sVer,
12✔
1824
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
12✔
1825
                           .allocatorId = pRequest->allocatorRefId,
12✔
1826
                           .parseSqlFp = clientParseSql,
1827
                           .parseSqlParam = pWrapper};
1828
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
12✔
1829
  (*pCxt)->biMode = biMode;
12✔
1830
  return TSDB_CODE_SUCCESS;
12✔
1831
}
1832

1833
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
12✔
1834
  STscStmt2*        pStmt = userdata;
12✔
1835
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
12✔
1836

1837
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
12✔
1838
  pStmt->affectedRows += pStmt->exec.affectedRows;
12✔
1839

1840
  fp(pStmt->options.userdata, res, code);
12✔
1841

1842
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
12!
1843
    taosUsleep(1);
×
1844
  }
1845
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
12✔
1846
  ++pStmt->sql.runTimes;
12✔
1847

1848
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
12!
1849
    tscError("failed to post asyncExecSem");
×
1850
  }
1851
}
12✔
1852

1853
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
4,455✔
1854
  STscStmt2* pStmt = (STscStmt2*)stmt;
4,455✔
1855
  int32_t    code = 0;
4,455✔
1856
  int64_t    startUs = taosGetTimestampUs();
4,456✔
1857

1858
  STMT_DLOG_E("start to exec");
4,456!
1859

1860
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4,456!
1861
    return pStmt->errCode;
×
1862
  }
1863

1864
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
4,456!
1865
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
4,458!
1866
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1867
  }
1868
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
4,455!
1869

1870
  if (pStmt->sql.stbInterlaceMode) {
4,458✔
1871
    STMT_ERR_RET(stmtAddBatch2(pStmt));
4,413!
1872
  }
1873

1874
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
4,457✔
1875

1876
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
4,454✔
1877
    if (pStmt->sql.stbInterlaceMode) {
4,452✔
1878
      int64_t startTs = taosGetTimestampUs();
4,410✔
1879
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
9,026✔
1880
        taosUsleep(1);
4,622✔
1881
      }
1882
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,406✔
1883

1884
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,406!
1885
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,407✔
1886
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,408✔
1887
      pStmt->sql.siInfo.pVgroupList = NULL;
4,408✔
1888
    } else {
1889
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
43✔
1890
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
41!
1891

1892
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
41!
1893

1894
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
41!
1895
    }
1896
  }
1897

1898
  SRequestObj*      pRequest = pStmt->exec.pRequest;
4,453✔
1899
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
4,453✔
1900

1901
  if (!fp) {
4,453✔
1902
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
4,441✔
1903

1904
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
4,439!
1905
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1906
      if (code) {
×
1907
        pStmt->exec.pRequest->code = code;
×
1908
      } else {
1909
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
1910
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1911
      }
1912
    }
1913

1914
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
4,441!
1915

1916
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
4,441✔
1917
    if (affected_rows) {
4,442✔
1918
      *affected_rows = pStmt->exec.affectedRows;
4,436✔
1919
    }
1920
    pStmt->affectedRows += pStmt->exec.affectedRows;
4,442✔
1921

1922
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
4,442!
1923
      taosUsleep(1);
×
1924
    }
1925

1926
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
4,439✔
1927

1928
    ++pStmt->sql.runTimes;
4,434✔
1929
  } else {
1930
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
12!
1931
    if (pWrapper == NULL) {
12!
1932
      code = terrno;
×
1933
    } else {
1934
      pWrapper->pRequest = pRequest;
12✔
1935
      pRequest->pWrapper = pWrapper;
12✔
1936
    }
1937
    if (TSDB_CODE_SUCCESS == code) {
12!
1938
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
12✔
1939
    }
1940
    pRequest->syncQuery = false;
12✔
1941
    pRequest->body.queryFp = asyncQueryCb;
12✔
1942
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
12✔
1943

1944
    pStmt->execSemWaited = false;
12✔
1945
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
12✔
1946
  }
1947

1948
_return:
4,446✔
1949
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
4,443✔
1950

1951
  STMT_RET(code);
4,443!
1952
}
1953

1954
int stmtClose2(TAOS_STMT2* stmt) {
142✔
1955
  STscStmt2* pStmt = (STscStmt2*)stmt;
142✔
1956

1957
  STMT_DLOG_E("start to free stmt");
142!
1958

1959
  if (pStmt->bindThreadInUse) {
142✔
1960
    pStmt->queue.stopQueue = true;
80✔
1961

1962
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
80✔
1963
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
80✔
1964
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
80✔
1965
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
80✔
1966

1967
    (void)taosThreadJoin(pStmt->bindThread, NULL);
80✔
1968
    pStmt->bindThreadInUse = false;
80✔
1969

1970
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
80✔
1971
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
80✔
1972
  }
1973

1974
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
142!
1975
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
142!
1976
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
1977
  }
1978
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
142!
1979

1980
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
142✔
1981
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
142✔
1982

1983
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
142!
1984
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
5!
1985
      tscError("failed to wait asyncExecSem");
×
1986
    }
1987
  }
1988

1989
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
142!
1990
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1991
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1992
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1993
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1994
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1995
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1996
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1997
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1998
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1999

2000
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
142!
2001

2002
  if (pStmt->options.asyncExecFn) {
142✔
2003
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
5!
2004
      tscError("failed to destroy asyncExecSem");
×
2005
    }
2006
  }
2007
  taosMemoryFree(stmt);
142!
2008

2009
  return TSDB_CODE_SUCCESS;
142✔
2010
}
2011

2012
const char* stmtErrstr2(TAOS_STMT2* stmt) {
3✔
2013
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
2014

2015
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
3!
2016
    return (char*)tstrerror(terrno);
3✔
2017
  }
2018

2019
  pStmt->exec.pRequest->code = terrno;
×
2020

2021
  return taos_errstr(pStmt->exec.pRequest);
×
2022
}
2023
/*
2024
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2025

2026
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2027
*/
2028

2029
int stmtParseColFields2(TAOS_STMT2* stmt) {
47✔
2030
  int32_t    code = 0;
47✔
2031
  STscStmt2* pStmt = (STscStmt2*)stmt;
47✔
2032
  int32_t    preCode = pStmt->errCode;
47✔
2033

2034
  STMT_DLOG_E("start to get col fields");
47!
2035

2036
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
47!
2037
    return pStmt->errCode;
×
2038
  }
2039

2040
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
47!
2041
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2042
  }
2043

2044
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
47!
2045

2046
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
47!
2047
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
3!
2048
    pStmt->bInfo.needParse = false;
×
2049
  }
2050
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
47✔
2051
    pStmt->bInfo.needParse = false;
7✔
2052
  }
2053
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
47!
2054
    taos_free_result(pStmt->exec.pRequest);
×
2055
    pStmt->exec.pRequest = NULL;
×
2056
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2057
  }
2058

2059
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
47!
2060

2061
  if (pStmt->bInfo.needParse) {
47✔
2062
    STMT_ERRI_JRET(stmtParseSql(pStmt));
40✔
2063
  }
2064

2065
_return:
32✔
2066
  // compatible with previous versions
2067
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
47!
2068
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
2069
  }
2070

2071
  pStmt->errCode = preCode;
47✔
2072

2073
  return code;
47✔
2074
}
2075

2076
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
47✔
2077
  int32_t code = stmtParseColFields2(stmt);
47✔
2078
  if (code != TSDB_CODE_SUCCESS) {
47✔
2079
    return code;
15✔
2080
  }
2081

2082
  return stmtFetchStbColFields2(stmt, nums, fields);
32✔
2083
}
2084

2085
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
9✔
2086
  int32_t    code = 0;
9✔
2087
  STscStmt2* pStmt = (STscStmt2*)stmt;
9✔
2088
  int32_t    preCode = pStmt->errCode;
9✔
2089

2090
  STMT_DLOG_E("start to get param num");
9!
2091

2092
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9!
2093
    return pStmt->errCode;
×
2094
  }
2095

2096
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
9!
2097

2098
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9!
2099
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2100
    pStmt->bInfo.needParse = false;
×
2101
  }
2102

2103
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
9!
2104
    taos_free_result(pStmt->exec.pRequest);
×
2105
    pStmt->exec.pRequest = NULL;
×
2106
  }
2107

2108
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
9!
2109

2110
  if (pStmt->bInfo.needParse) {
9!
2111
    STMT_ERRI_JRET(stmtParseSql(pStmt));
9✔
2112
  }
2113

2114
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2!
2115
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
2✔
2116
  } else {
2117
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2118
  }
2119

2120
_return:
×
2121

2122
  pStmt->errCode = preCode;
9✔
2123

2124
  return code;
9✔
2125
}
2126

2127
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
3✔
2128
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
2129

2130
  STMT_DLOG_E("start to use result");
3!
2131

2132
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
3!
2133
    tscError("useResult only for query statement");
×
2134
    return NULL;
×
2135
  }
2136

2137
  return pStmt->exec.pRequest;
3✔
2138
}
2139

2140
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2141
  qInfo("async stmt bind thread started");
×
2142

2143
  ThreadArgs* targs = (ThreadArgs*)args;
×
2144
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2145

2146
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2147
  targs->fp(targs->param, NULL, code);
×
2148
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2149
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2150
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2151
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2152
  taosMemoryFree(args);
×
2153

2154
  qInfo("async stmt bind thread stopped");
×
2155

2156
  return code;
×
2157
}
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