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

taosdata / TDengine / #4312

14 Jun 2025 02:06PM UTC coverage: 62.846% (+0.4%) from 62.454%
#4312

push

travis-ci

web-flow
fix: taosdump windows pthread_mutex_unlock crash(3.0) (#31357)

* fix: windows pthread_mutex_unlock crash

* enh: sync from main fix taosdump crash windows

* fix: restore .github action branch to main

157955 of 320091 branches covered (49.35%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 2 files covered. (25.0%)

68 existing lines in 33 files now uncovered.

243588 of 318845 relevant lines covered (76.4%)

7256651.73 hits per line

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

63.98
/source/client/src/clientImpl.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "cJSON.h"
17
#include "clientInt.h"
18
#include "clientLog.h"
19
#include "clientMonitor.h"
20
#include "command.h"
21
#include "decimal.h"
22
#include "scheduler.h"
23
#include "tdatablock.h"
24
#include "tdataformat.h"
25
#include "tdef.h"
26
#include "tglobal.h"
27
#include "tmsgtype.h"
28
#include "tpagedbuf.h"
29
#include "tref.h"
30
#include "tsched.h"
31
#include "tversion.h"
32

33
static int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet);
34
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo);
35

36
void setQueryRequest(int64_t rId) {
1,482,150✔
37
  SRequestObj* pReq = acquireRequest(rId);
1,482,150✔
38
  if (pReq != NULL) {
1,482,155✔
39
    pReq->isQuery = true;
1,482,138✔
40
    (void)releaseRequest(rId);
1,482,138✔
41
  }
42
}
1,482,147✔
43

44
static bool stringLengthCheck(const char* str, size_t maxsize) {
63,872✔
45
  if (str == NULL) {
63,872!
46
    return false;
×
47
  }
48

49
  size_t len = strlen(str);
63,872✔
50
  if (len <= 0 || len > maxsize) {
63,872!
51
    return false;
×
52
  }
53

54
  return true;
63,896✔
55
}
56

57
static bool validateUserName(const char* user) { return stringLengthCheck(user, TSDB_USER_LEN - 1); }
31,178✔
58

59
static bool validatePassword(const char* passwd) { return stringLengthCheck(passwd, TSDB_PASSWORD_MAX_LEN); }
31,163✔
60

61
static bool validateDbName(const char* db) { return stringLengthCheck(db, TSDB_DB_NAME_LEN - 1); }
1,547✔
62

63
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
31,087✔
64
  char key[512] = {0};
31,087✔
65
  (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
31,087✔
66
  return taosStrdup(key);
31,087!
67
}
68

69
bool chkRequestKilled(void* param) {
12,781,849✔
70
  bool         killed = false;
12,781,849✔
71
  SRequestObj* pRequest = acquireRequest((int64_t)param);
12,781,849✔
72
  if (NULL == pRequest || pRequest->killed) {
12,807,970!
73
    killed = true;
×
74
  }
75

76
  (void)releaseRequest((int64_t)param);
12,807,970✔
77

78
  return killed;
12,807,861✔
79
}
80

81
void cleanupAppInfo() {
18,108✔
82
  taosHashCleanup(appInfo.pInstMap);
18,108✔
83
  taosHashCleanup(appInfo.pInstMapByClusterId);
18,108✔
84
  tscInfo("cluster instance map cleaned");
18,108!
85
}
18,108✔
86

87
static int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param,
88
                               SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj);
89

90
int32_t taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db,
31,183✔
91
                              uint16_t port, int connType, STscObj** pObj) {
92
  TSC_ERR_RET(taos_init());
31,183!
93
  if (!validateUserName(user)) {
31,183!
94
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
95
  }
96
  int32_t code = 0;
31,179✔
97

98
  char localDb[TSDB_DB_NAME_LEN] = {0};
31,179✔
99
  if (db != NULL && strlen(db) > 0) {
31,179✔
100
    if (!validateDbName(db)) {
1,547!
101
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
102
    }
103

104
    tstrncpy(localDb, db, sizeof(localDb));
1,547✔
105
    (void)strdequote(localDb);
1,547✔
106
  }
107

108
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
31,168✔
109
  if (auth == NULL) {
31,168✔
110
    if (!validatePassword(pass)) {
31,164!
111
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_PASS_LENGTH);
×
112
    }
113

114
    taosEncryptPass_c((uint8_t*)pass, strlen(pass), secretEncrypt);
31,181✔
115
  } else {
116
    tstrncpy(secretEncrypt, auth, tListLen(secretEncrypt));
4✔
117
  }
118

119
  SCorEpSet epSet = {0};
31,141✔
120
  if (ip) {
31,141✔
121
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
9,673✔
122
  } else {
123
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
21,468!
124
  }
125

126
  if (port) {
31,086✔
127
    epSet.epSet.eps[0].port = port;
690✔
128
    epSet.epSet.eps[1].port = port;
690✔
129
  }
130

131
  char* key = getClusterKey(user, secretEncrypt, ip, port);
31,086✔
132
  if (NULL == key) {
31,114!
133
    TSC_ERR_RET(terrno);
×
134
  }
135
  tscInfo("connecting to server, numOfEps:%d inUse:%d user:%s db:%s key:%s", epSet.epSet.numOfEps, epSet.epSet.inUse,
31,114✔
136
          user, db, key);
137
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
83,836✔
138
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
52,646!
139
  }
140
  // for (int32_t i = 0; i < epSet.epSet.numOfEps; i++) {
141
  //   if ((code = taosValidFqdn(tsEnableIpv6, epSet.epSet.eps[i].fqdn)) != 0) {
142
  //     taosMemFree(key);
143
  //     tscError("ipv6 flag %d, the local FQDN %s does not resolve to the ip address since %s", tsEnableIpv6,
144
  //              epSet.epSet.eps[i].fqdn, tstrerror(code));
145
  //     TSC_ERR_RET(code);
146
  //   }
147
  // }
148

149
  SAppInstInfo** pInst = NULL;
31,190✔
150
  code = taosThreadMutexLock(&appInfo.mutex);
31,190✔
151
  if (TSDB_CODE_SUCCESS != code) {
31,191!
152
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
153
    TSC_ERR_RET(code);
×
154
  }
155

156
  pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
31,191✔
157
  SAppInstInfo* p = NULL;
31,191✔
158
  if (pInst == NULL) {
31,191✔
159
    p = taosMemoryCalloc(1, sizeof(struct SAppInstInfo));
18,365!
160
    if (NULL == p) {
18,365!
161
      TSC_ERR_JRET(terrno);
×
162
    }
163
    p->mgmtEp = epSet;
18,365✔
164
    code = taosThreadMutexInit(&p->qnodeMutex, NULL);
18,365✔
165
    if (TSDB_CODE_SUCCESS != code) {
18,365!
166
      taosMemoryFree(p);
×
167
      TSC_ERR_JRET(code);
×
168
    }
169
    code = openTransporter(user, secretEncrypt, tsNumOfCores / 2, &p->pTransporter);
18,365✔
170
    if (TSDB_CODE_SUCCESS != code) {
18,365!
171
      taosMemoryFree(p);
×
172
      TSC_ERR_JRET(code);
×
173
    }
174
    code = appHbMgrInit(p, key, &p->pAppHbMgr);
18,365✔
175
    if (TSDB_CODE_SUCCESS != code) {
18,365!
176
      destroyAppInst(&p);
×
177
      TSC_ERR_JRET(code);
×
178
    }
179
    code = taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
18,365✔
180
    if (TSDB_CODE_SUCCESS != code) {
18,365!
181
      destroyAppInst(&p);
×
182
      TSC_ERR_JRET(code);
×
183
    }
184
    p->instKey = key;
18,365✔
185
    key = NULL;
18,365✔
186
    tscInfo("new app inst mgr:%p, user:%s, ip:%s, port:%d", p, user, epSet.epSet.eps[0].fqdn, epSet.epSet.eps[0].port);
18,365!
187

188
    pInst = &p;
18,365✔
189
  } else {
190
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
12,826!
191
      tscError("*pInst:%p, pAppHgMgr:%p", *pInst, (*pInst) ? (*pInst)->pAppHbMgr : NULL);
×
192
      TSC_ERR_JRET(TSDB_CODE_TSC_INTERNAL_ERROR);
×
193
    }
194
    // reset to 0 in case of conn with duplicated user key but its user has ever been dropped.
195
    atomic_store_8(&(*pInst)->pAppHbMgr->connHbFlag, 0);
12,826✔
196
  }
197

198
_return:
31,191✔
199

200
  if (TSDB_CODE_SUCCESS != code) {
31,191!
201
    (void)taosThreadMutexUnlock(&appInfo.mutex);
×
202
    taosMemoryFreeClear(key);
×
203
    return code;
×
204
  } else {
205
    code = taosThreadMutexUnlock(&appInfo.mutex);
31,191✔
206
    taosMemoryFreeClear(key);
31,189!
207
    if (TSDB_CODE_SUCCESS != code) {
31,189!
208
      tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
209
      return code;
×
210
    }
211
    return taosConnectImpl(user, &secretEncrypt[0], localDb, NULL, NULL, *pInst, connType, pObj);
31,189✔
212
  }
213
}
214

215
// SAppInstInfo* getAppInstInfo(const char* clusterKey) {
216
//   SAppInstInfo** ppAppInstInfo = taosHashGet(appInfo.pInstMap, clusterKey, strlen(clusterKey));
217
//   if (ppAppInstInfo != NULL && *ppAppInstInfo != NULL) {
218
//     return *ppAppInstInfo;
219
//   } else {
220
//     return NULL;
221
//   }
222
// }
223

224
void freeQueryParam(SSyncQueryParam* param) {
1,201✔
225
  if (param == NULL) return;
1,201!
226
  if (TSDB_CODE_SUCCESS != tsem_destroy(&param->sem)) {
1,201!
227
    tscError("failed to destroy semaphore in freeQueryParam");
×
228
  }
229
  taosMemoryFree(param);
1,201!
230
}
231

232
int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql,
3,211,887✔
233
                     SRequestObj** pRequest, int64_t reqid) {
234
  int32_t code = createRequest(connId, TSDB_SQL_SELECT, reqid, pRequest);
3,211,887✔
235
  if (TSDB_CODE_SUCCESS != code) {
3,212,107!
236
    tscError("failed to malloc sqlObj, %s", sql);
×
237
    return code;
×
238
  }
239

240
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
3,212,107!
241
  if ((*pRequest)->sqlstr == NULL) {
3,212,059!
242
    tscError("req:0x%" PRIx64 ", failed to prepare sql string buffer, %s", (*pRequest)->self, sql);
×
243
    destroyRequest(*pRequest);
×
244
    *pRequest = NULL;
×
245
    return terrno;
×
246
  }
247

248
  (void)strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen);
3,212,059✔
249
  (*pRequest)->sqlstr[sqlLen] = 0;
3,212,096✔
250
  (*pRequest)->sqlLen = sqlLen;
3,212,096✔
251
  (*pRequest)->validateOnly = validateSql;
3,212,096✔
252
  (*pRequest)->isStmtBind = false;
3,212,096✔
253

254
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
3,212,096✔
255

256
  STscObj* pTscObj = (*pRequest)->pTscObj;
3,212,096✔
257
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
3,212,096✔
258
                             sizeof((*pRequest)->self));
259
  if (err) {
3,212,144!
260
    tscError("req:0x%" PRId64 ", failed to add to request container, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
261
             (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
262
    destroyRequest(*pRequest);
×
263
    *pRequest = NULL;
×
264
    return terrno;
×
265
  }
266

267
  (*pRequest)->allocatorRefId = -1;
3,212,144✔
268
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
3,212,144✔
269
    if (TSDB_CODE_SUCCESS !=
1,253,229!
270
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
1,253,183✔
271
      tscError("req:0x%" PRId64 ", failed to create node allocator, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
272
               (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
273
      destroyRequest(*pRequest);
×
274
      *pRequest = NULL;
×
275
      return terrno;
×
276
    }
277
  }
278

279
  tscDebug("req:0x%" PRIx64 ", build request, QID:0x%" PRIx64, (*pRequest)->self, (*pRequest)->requestId);
3,212,177✔
280
  return TSDB_CODE_SUCCESS;
3,212,034✔
281
}
282

283
int32_t buildPreviousRequest(SRequestObj* pRequest, const char* sql, SRequestObj** pNewRequest) {
459✔
284
  int32_t code =
285
      buildRequest(pRequest->pTscObj->id, sql, strlen(sql), pRequest, pRequest->validateOnly, pNewRequest, 0);
459✔
286
  if (TSDB_CODE_SUCCESS == code) {
459!
287
    pRequest->relation.prevRefId = (*pNewRequest)->self;
459✔
288
    (*pNewRequest)->relation.nextRefId = pRequest->self;
459✔
289
    (*pNewRequest)->relation.userRefId = pRequest->self;
459✔
290
    (*pNewRequest)->isSubReq = true;
459✔
291
  }
292
  return code;
459✔
293
}
294

295
int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtCallback* pStmtCb) {
20,864✔
296
  STscObj* pTscObj = pRequest->pTscObj;
20,864✔
297

298
  SParseContext cxt = {
20,864✔
299
      .requestId = pRequest->requestId,
20,864✔
300
      .requestRid = pRequest->self,
20,864✔
301
      .acctId = pTscObj->acctId,
20,864✔
302
      .db = pRequest->pDb,
20,864✔
303
      .topicQuery = topicQuery,
304
      .pSql = pRequest->sqlstr,
20,864✔
305
      .sqlLen = pRequest->sqlLen,
20,864✔
306
      .pMsg = pRequest->msgBuf,
20,864✔
307
      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
308
      .pTransporter = pTscObj->pAppInfo->pTransporter,
20,864✔
309
      .pStmtCb = pStmtCb,
310
      .pUser = pTscObj->user,
20,864✔
311
      .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
20,864✔
312
      .enableSysInfo = pTscObj->sysInfo,
20,864✔
313
      .svrVer = pTscObj->sVer,
20,864✔
314
      .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
20,864✔
315
      .isStmtBind = pRequest->isStmtBind,
20,864✔
316
      .setQueryFp = setQueryRequest,
317
      .timezone = pTscObj->optionInfo.timezone,
20,864✔
318
      .charsetCxt = pTscObj->optionInfo.charsetCxt,
20,864✔
319
  };
320

321
  cxt.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
20,864✔
322
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.pCatalog);
20,901✔
323
  if (code != TSDB_CODE_SUCCESS) {
20,896!
324
    return code;
×
325
  }
326

327
  code = qParseSql(&cxt, pQuery);
20,896✔
328
  if (TSDB_CODE_SUCCESS == code) {
20,877✔
329
    if ((*pQuery)->haveResultSet) {
20,851!
330
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols,
×
331
                              (*pQuery)->pResExtSchema, pRequest->isStmtBind);
×
332
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
×
333
    }
334
  }
335

336
  if (TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) {
20,889!
337
    TSWAP(pRequest->dbList, (*pQuery)->pDbList);
20,853✔
338
    TSWAP(pRequest->tableList, (*pQuery)->pTableList);
20,853✔
339
    TSWAP(pRequest->targetTableList, (*pQuery)->pTargetTableList);
20,853✔
340
  }
341

342
  taosArrayDestroy(cxt.pTableMetaPos);
20,889✔
343
  taosArrayDestroy(cxt.pTableVgroupPos);
20,883✔
344

345
  return code;
20,895✔
346
}
347

348
int32_t execLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
×
349
  SRetrieveTableRsp* pRsp = NULL;
×
350
  int8_t             biMode = atomic_load_8(&pRequest->pTscObj->biMode);
×
351
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, pQuery->pRoot, &pRsp, biMode,
×
352
                              pRequest->pTscObj->optionInfo.charsetCxt);
×
353
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
×
354
    code =
355
        setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4, pRequest->isStmtBind);
×
356
  }
357

358
  return code;
×
359
}
360

361
int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
1,162✔
362
  // drop table if exists not_exists_table
363
  if (NULL == pQuery->pCmdMsg) {
1,162!
364
    return TSDB_CODE_SUCCESS;
×
365
  }
366

367
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
1,162✔
368
  pRequest->type = pMsgInfo->msgType;
1,162✔
369
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
1,162✔
370
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
1,162✔
371

372
  STscObj*      pTscObj = pRequest->pTscObj;
1,162✔
373
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
1,162✔
374

375
  // int64_t transporterId = 0;
376
  TSC_ERR_RET(asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg));
1,160!
377
  TSC_ERR_RET(tsem_wait(&pRequest->body.rspSem));
1,165!
378
  return TSDB_CODE_SUCCESS;
1,165✔
379
}
380

381
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
5,688,963✔
382

383
void asyncExecLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
125,905✔
384
  SRetrieveTableRsp* pRsp = NULL;
125,905✔
385
  if (pRequest->validateOnly) {
125,905✔
386
    doRequestCallback(pRequest, 0);
27✔
387
    return;
27✔
388
  }
389

390
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, pQuery->pRoot, &pRsp,
125,878✔
391
                              atomic_load_8(&pRequest->pTscObj->biMode), pRequest->pTscObj->optionInfo.charsetCxt);
125,878✔
392
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
125,877✔
393
    code =
394
        setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4, pRequest->isStmtBind);
115,401✔
395
  }
396

397
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
125,878✔
398
  pRequest->code = code;
125,878✔
399

400
  if (pRequest->code != TSDB_CODE_SUCCESS) {
125,878✔
401
    pResultInfo->numOfRows = 0;
14✔
402
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
14!
403
             pRequest->requestId);
404
  } else {
405
    tscDebug(
125,864✔
406
        "req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
407
        pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed, pRequest->requestId);
408
  }
409

410
  doRequestCallback(pRequest, code);
125,878✔
411
}
412

413
int32_t asyncExecDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
42,118✔
414
  if (pRequest->validateOnly) {
42,118!
415
    doRequestCallback(pRequest, 0);
×
416
    return TSDB_CODE_SUCCESS;
×
417
  }
418

419
  // drop table if exists not_exists_table
420
  if (NULL == pQuery->pCmdMsg) {
42,118✔
421
    doRequestCallback(pRequest, 0);
1✔
422
    return TSDB_CODE_SUCCESS;
1✔
423
  }
424

425
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
42,117✔
426
  pRequest->type = pMsgInfo->msgType;
42,117✔
427
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
42,117✔
428
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
42,117✔
429

430
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
42,117✔
431
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
42,111✔
432

433
  int32_t code = asyncSendMsgToServer(pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg);
42,121✔
434
  if (code) {
42,146!
435
    doRequestCallback(pRequest, code);
×
436
  }
437
  return code;
42,147✔
438
}
439

440
int compareQueryNodeLoad(const void* elem1, const void* elem2) {
366✔
441
  SQueryNodeLoad* node1 = (SQueryNodeLoad*)elem1;
366✔
442
  SQueryNodeLoad* node2 = (SQueryNodeLoad*)elem2;
366✔
443

444
  if (node1->load < node2->load) {
366!
445
    return -1;
×
446
  }
447

448
  return node1->load > node2->load;
366✔
449
}
450

451
int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) {
23,043✔
452
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
23,043!
453
  if (pInfo->pQnodeList) {
23,043✔
454
    taosArrayDestroy(pInfo->pQnodeList);
22,399✔
455
    pInfo->pQnodeList = NULL;
22,399✔
456
    tscDebug("QnodeList cleared in cluster 0x%" PRIx64, pInfo->clusterId);
22,399✔
457
  }
458

459
  if (pNodeList) {
23,043!
460
    pInfo->pQnodeList = taosArrayDup(pNodeList, NULL);
23,043✔
461
    taosArraySort(pInfo->pQnodeList, compareQueryNodeLoad);
23,043✔
462
    tscDebug("QnodeList updated in cluster 0x%" PRIx64 ", num:%ld", pInfo->clusterId,
23,043✔
463
             taosArrayGetSize(pInfo->pQnodeList));
464
  }
465
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
23,043!
466

467
  return TSDB_CODE_SUCCESS;
23,043✔
468
}
469

470
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
3,178,615✔
471
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
3,178,615✔
472
    *required = false;
2,522,457✔
473
    return TSDB_CODE_SUCCESS;
2,522,457✔
474
  }
475

476
  int32_t       code = TSDB_CODE_SUCCESS;
656,158✔
477
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
656,158✔
478
  *required = false;
656,158✔
479

480
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
656,158!
481
  *required = (NULL == pInfo->pQnodeList);
656,158✔
482
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
656,158!
483
  return TSDB_CODE_SUCCESS;
656,158✔
484
}
485

486
int32_t getQnodeList(SRequestObj* pRequest, SArray** pNodeList) {
×
487
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
×
488
  int32_t       code = 0;
×
489

490
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
×
491
  if (pInfo->pQnodeList) {
×
492
    *pNodeList = taosArrayDup(pInfo->pQnodeList, NULL);
×
493
  }
494
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
×
495
  if (NULL == *pNodeList) {
×
496
    SCatalog* pCatalog = NULL;
×
497
    code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
×
498
    if (TSDB_CODE_SUCCESS == code) {
×
499
      *pNodeList = taosArrayInit(5, sizeof(SQueryNodeLoad));
×
500
      if (NULL == pNodeList) {
×
501
        TSC_ERR_RET(terrno);
×
502
      }
503
      SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
×
504
                               .requestId = pRequest->requestId,
×
505
                               .requestObjRefId = pRequest->self,
×
506
                               .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
×
507
      code = catalogGetQnodeList(pCatalog, &conn, *pNodeList);
×
508
    }
509

510
    if (TSDB_CODE_SUCCESS == code && *pNodeList) {
×
511
      code = updateQnodeList(pInfo, *pNodeList);
×
512
    }
513
  }
514

515
  return code;
×
516
}
517

518
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList) {
64,377✔
519
  pRequest->type = pQuery->msgType;
64,377✔
520
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
64,377✔
521

522
  SPlanContext cxt = {.queryId = pRequest->requestId,
128,848✔
523
                      .acctId = pRequest->pTscObj->acctId,
64,392✔
524
                      .mgmtEpSet = getEpSet_s(&pAppInfo->mgmtEp),
64,392✔
525
                      .pAstRoot = pQuery->pRoot,
64,456✔
526
                      .showRewrite = pQuery->showRewrite,
64,456✔
527
                      .pMsg = pRequest->msgBuf,
64,456✔
528
                      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
529
                      .pUser = pRequest->pTscObj->user,
64,456✔
530
                      .timezone = pRequest->pTscObj->optionInfo.timezone,
64,456✔
531
                      .sysInfo = pRequest->pTscObj->sysInfo};
64,456✔
532

533
  return qCreateQueryPlan(&cxt, pPlan, pNodeList);
64,456✔
534
}
535

536
int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols,
1,276,835✔
537
                         const SExtSchema* pExtSchema, bool isStmt) {
538
  if (pResInfo == NULL || pSchema == NULL || numOfCols <= 0) {
1,276,835!
539
    tscError("invalid paras, pResInfo == NULL || pSchema == NULL || numOfCols <= 0");
×
540
    return TSDB_CODE_INVALID_PARA;
×
541
  }
542

543
  pResInfo->numOfCols = numOfCols;
1,276,850✔
544
  if (pResInfo->fields != NULL) {
1,276,850✔
545
    taosMemoryFree(pResInfo->fields);
46!
546
  }
547
  if (pResInfo->userFields != NULL) {
1,276,850✔
548
    taosMemoryFree(pResInfo->userFields);
46!
549
  }
550
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
1,276,850!
551
  if (NULL == pResInfo->fields) return terrno;
1,276,846!
552
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
1,276,846!
553
  if (NULL == pResInfo->userFields) {
1,276,849!
554
    taosMemoryFree(pResInfo->fields);
×
555
    return terrno;
×
556
  }
557
  if (numOfCols != pResInfo->numOfCols) {
1,276,849!
558
    tscError("numOfCols:%d != pResInfo->numOfCols:%d", numOfCols, pResInfo->numOfCols);
×
559
    return TSDB_CODE_FAILED;
×
560
  }
561

562
  for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
6,565,626✔
563
    pResInfo->fields[i].type = pSchema[i].type;
5,288,829✔
564

565
    pResInfo->userFields[i].type = pSchema[i].type;
5,288,829✔
566
    // userFields must convert to type bytes, no matter isStmt or not
567
    pResInfo->userFields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, false);
5,288,829✔
568
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
5,288,753✔
569
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
5,288,724!
570
      decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale);
8,559✔
571
    }
572

573
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
5,288,777✔
574
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
5,288,777✔
575
  }
576
  return TSDB_CODE_SUCCESS;
1,276,797✔
577
}
578

579
void setResPrecision(SReqResultInfo* pResInfo, int32_t precision) {
922,670✔
580
  if (precision != TSDB_TIME_PRECISION_MILLI && precision != TSDB_TIME_PRECISION_MICRO &&
922,670!
581
      precision != TSDB_TIME_PRECISION_NANO) {
582
    return;
×
583
  }
584

585
  pResInfo->precision = precision;
922,670✔
586
}
587

588
int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) {
580,653✔
589
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
580,653✔
590
  if (NULL == nodeList) {
580,742!
591
    return terrno;
×
592
  }
593
  char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client";
580,752✔
594

595
  int32_t dbNum = taosArrayGetSize(pDbVgList);
580,752✔
596
  for (int32_t i = 0; i < dbNum; ++i) {
1,134,893✔
597
    SArray* pVg = taosArrayGetP(pDbVgList, i);
554,119✔
598
    if (NULL == pVg) {
554,127!
599
      continue;
×
600
    }
601
    int32_t vgNum = taosArrayGetSize(pVg);
554,127✔
602
    if (vgNum <= 0) {
554,143✔
603
      continue;
646✔
604
    }
605

606
    for (int32_t j = 0; j < vgNum; ++j) {
1,963,092✔
607
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
1,409,574✔
608
      if (NULL == pInfo) {
1,409,582!
609
        taosArrayDestroy(nodeList);
×
610
        return TSDB_CODE_OUT_OF_RANGE;
×
611
      }
612
      SQueryNodeLoad load = {0};
1,409,582✔
613
      load.addr.nodeId = pInfo->vgId;
1,409,582✔
614
      load.addr.epSet = pInfo->epSet;
1,409,582✔
615

616
      if (NULL == taosArrayPush(nodeList, &load)) {
1,409,595!
617
        taosArrayDestroy(nodeList);
×
618
        return terrno;
×
619
      }
620
    }
621
  }
622

623
  int32_t vnodeNum = taosArrayGetSize(nodeList);
580,774✔
624
  if (vnodeNum > 0) {
580,783✔
625
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
552,626✔
626
    goto _return;
552,625✔
627
  }
628

629
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
28,157✔
630
  if (mnodeNum <= 0) {
28,143!
631
    tscDebug("0x%" PRIx64 " %s policy, empty node list", pRequest->requestId, policy);
×
632
    goto _return;
×
633
  }
634

635
  void* pData = taosArrayGet(pMnodeList, 0);
28,143✔
636
  if (NULL == pData) {
28,139!
637
    taosArrayDestroy(nodeList);
×
638
    return TSDB_CODE_OUT_OF_RANGE;
×
639
  }
640
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
28,139!
641
    taosArrayDestroy(nodeList);
×
642
    return terrno;
×
643
  }
644

645
  tscDebug("0x%" PRIx64 " %s policy, use mnode list, num:%d", pRequest->requestId, policy, mnodeNum);
28,144✔
646

647
_return:
21,642✔
648

649
  *pNodeList = nodeList;
580,745✔
650

651
  return TSDB_CODE_SUCCESS;
580,745✔
652
}
653

654
int32_t buildQnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pQnodeList) {
295,462✔
655
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
295,462✔
656
  if (NULL == nodeList) {
295,462!
657
    return terrno;
×
658
  }
659

660
  int32_t qNodeNum = taosArrayGetSize(pQnodeList);
295,462✔
661
  if (qNodeNum > 0) {
295,462✔
662
    void* pData = taosArrayGet(pQnodeList, 0);
295,140✔
663
    if (NULL == pData) {
295,140!
664
      taosArrayDestroy(nodeList);
×
665
      return TSDB_CODE_OUT_OF_RANGE;
×
666
    }
667
    if (NULL == taosArrayAddBatch(nodeList, pData, qNodeNum)) {
295,140!
668
      taosArrayDestroy(nodeList);
×
669
      return terrno;
×
670
    }
671
    tscDebug("0x%" PRIx64 " qnode policy, use qnode list, num:%d", pRequest->requestId, qNodeNum);
295,140✔
672
    goto _return;
295,140✔
673
  }
674

675
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
322✔
676
  if (mnodeNum <= 0) {
322✔
677
    tscDebug("0x%" PRIx64 " qnode policy, empty node list", pRequest->requestId);
4!
678
    goto _return;
4✔
679
  }
680

681
  void* pData = taosArrayGet(pMnodeList, 0);
318✔
682
  if (NULL == pData) {
318!
683
    taosArrayDestroy(nodeList);
×
684
    return TSDB_CODE_OUT_OF_RANGE;
×
685
  }
686
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
318!
687
    taosArrayDestroy(nodeList);
×
688
    return terrno;
×
689
  }
690

691
  tscDebug("0x%" PRIx64 " qnode policy, use mnode list, num:%d", pRequest->requestId, mnodeNum);
318!
692

693
_return:
×
694

695
  *pNodeList = nodeList;
295,462✔
696

697
  return TSDB_CODE_SUCCESS;
295,462✔
698
}
699

700
void freeVgList(void* list) {
44,037✔
701
  SArray* pList = *(SArray**)list;
44,037✔
702
  taosArrayDestroy(pList);
44,037✔
703
}
44,093✔
704

705
int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SMetaData* pResultMeta) {
811,761✔
706
  SArray* pDbVgList = NULL;
811,761✔
707
  SArray* pQnodeList = NULL;
811,761✔
708
  FDelete fp = NULL;
811,761✔
709
  int32_t code = 0;
811,761✔
710

711
  switch (tsQueryPolicy) {
811,761!
712
    case QUERY_POLICY_VNODE:
516,309✔
713
    case QUERY_POLICY_CLIENT: {
714
      if (pResultMeta) {
516,309!
715
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
516,323✔
716
        if (NULL == pDbVgList) {
516,315!
717
          code = terrno;
×
718
          goto _return;
×
719
        }
720
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
516,315✔
721
        for (int32_t i = 0; i < dbNum; ++i) {
1,026,392✔
722
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
510,068✔
723
          if (pRes->code || NULL == pRes->pRes) {
510,066!
724
            continue;
×
725
          }
726

727
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
1,020,146!
728
            code = terrno;
×
729
            goto _return;
×
730
          }
731
        }
732
      } else {
733
        fp = freeVgList;
×
734

735
        int32_t dbNum = taosArrayGetSize(pRequest->dbList);
×
736
        if (dbNum > 0) {
×
737
          SCatalog*     pCtg = NULL;
×
738
          SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
×
739
          code = catalogGetHandle(pInst->clusterId, &pCtg);
×
740
          if (code != TSDB_CODE_SUCCESS) {
×
741
            goto _return;
×
742
          }
743

744
          pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
×
745
          if (NULL == pDbVgList) {
×
746
            code = terrno;
×
747
            goto _return;
×
748
          }
749
          SArray* pVgList = NULL;
×
750
          for (int32_t i = 0; i < dbNum; ++i) {
×
751
            char*            dbFName = taosArrayGet(pRequest->dbList, i);
×
752
            SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
×
753
                                     .requestId = pRequest->requestId,
×
754
                                     .requestObjRefId = pRequest->self,
×
755
                                     .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
×
756

757
            // catalogGetDBVgList will handle dbFName == null.
758
            code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
×
759
            if (code) {
×
760
              goto _return;
×
761
            }
762

763
            if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
×
764
              code = terrno;
×
765
              goto _return;
×
766
            }
767
          }
768
        }
769
      }
770

771
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
516,324✔
772
      break;
516,326✔
773
    }
774
    case QUERY_POLICY_HYBRID:
295,456✔
775
    case QUERY_POLICY_QNODE: {
776
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
300,899!
777
        SMetaRes* pRes = taosArrayGet(pResultMeta->pQnodeList, 0);
5,443✔
778
        if (pRes->code) {
5,443!
779
          pQnodeList = NULL;
×
780
        } else {
781
          pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL);
5,443✔
782
          if (NULL == pQnodeList) {
5,443!
783
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
784
            goto _return;
×
785
          }
786
        }
787
      } else {
788
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
290,019✔
789
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
290,019!
790
        if (pInst->pQnodeList) {
290,019!
791
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
290,019✔
792
          if (NULL == pQnodeList) {
290,019!
793
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
794
            goto _return;
×
795
          }
796
        }
797
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
290,019!
798
      }
799

800
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
295,462✔
801
      break;
295,462✔
802
    }
803
    default:
×
804
      tscError("unknown query policy: %d", tsQueryPolicy);
×
805
      return TSDB_CODE_APP_ERROR;
×
806
  }
807

808
_return:
811,788✔
809
  taosArrayDestroyEx(pDbVgList, fp);
811,788✔
810
  taosArrayDestroy(pQnodeList);
811,788✔
811

812
  return code;
811,790✔
813
}
814

815
int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList) {
64,337✔
816
  SArray* pDbVgList = NULL;
64,337✔
817
  SArray* pQnodeList = NULL;
64,337✔
818
  int32_t code = 0;
64,337✔
819

820
  switch (tsQueryPolicy) {
64,337!
821
    case QUERY_POLICY_VNODE:
64,350✔
822
    case QUERY_POLICY_CLIENT: {
823
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
64,350✔
824
      if (dbNum > 0) {
64,424✔
825
        SCatalog*     pCtg = NULL;
44,086✔
826
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
44,086✔
827
        code = catalogGetHandle(pInst->clusterId, &pCtg);
44,086✔
828
        if (code != TSDB_CODE_SUCCESS) {
44,071!
829
          goto _return;
×
830
        }
831

832
        pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
44,071✔
833
        if (NULL == pDbVgList) {
44,088✔
834
          code = terrno;
26✔
835
          goto _return;
×
836
        }
837
        SArray* pVgList = NULL;
44,062✔
838
        for (int32_t i = 0; i < dbNum; ++i) {
88,146✔
839
          char*            dbFName = taosArrayGet(pRequest->dbList, i);
44,015✔
840
          SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
44,042✔
841
                                   .requestId = pRequest->requestId,
44,042✔
842
                                   .requestObjRefId = pRequest->self,
44,042✔
843
                                   .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
44,042✔
844

845
          // catalogGetDBVgList will handle dbFName == null.
846
          code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
44,105✔
847
          if (code) {
44,089!
848
            goto _return;
×
849
          }
850

851
          if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
44,084!
852
            code = terrno;
×
853
            goto _return;
×
854
          }
855
        }
856
      }
857

858
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
64,469✔
859
      break;
64,415✔
860
    }
861
    case QUERY_POLICY_HYBRID:
×
862
    case QUERY_POLICY_QNODE: {
863
      TSC_ERR_JRET(getQnodeList(pRequest, &pQnodeList));
×
864

865
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
×
866
      break;
×
867
    }
868
    default:
×
869
      tscError("unknown query policy: %d", tsQueryPolicy);
×
870
      return TSDB_CODE_APP_ERROR;
×
871
  }
872

873
_return:
64,415✔
874

875
  taosArrayDestroyEx(pDbVgList, freeVgList);
64,415✔
876
  taosArrayDestroy(pQnodeList);
64,423✔
877

878
  return code;
64,438✔
879
}
880

881
int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) {
64,365✔
882
  void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter;
64,365✔
883

884
  SExecResult      res = {0};
64,365✔
885
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
64,365✔
886
                           .requestId = pRequest->requestId,
64,365✔
887
                           .requestObjRefId = pRequest->self};
64,365✔
888
  SSchedulerReq    req = {
128,746✔
889
         .syncReq = true,
890
         .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
64,365✔
891
         .pConn = &conn,
892
         .pNodeList = pNodeList,
893
         .pDag = pDag,
894
         .sql = pRequest->sqlstr,
64,365✔
895
         .startTs = pRequest->metric.start,
64,365✔
896
         .execFp = NULL,
897
         .cbParam = NULL,
898
         .chkKillFp = chkRequestKilled,
899
         .chkKillParam = (void*)pRequest->self,
64,365✔
900
         .pExecRes = &res,
901
         .source = pRequest->source,
64,365✔
902
         .pWorkerCb = getTaskPoolWorkerCb(),
64,365✔
903
  };
904

905
  int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob);
64,381✔
906

907
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
64,389✔
908
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
64,383✔
909

910
  if (code != TSDB_CODE_SUCCESS) {
64,383!
911
    schedulerFreeJob(&pRequest->body.queryJob, 0);
×
912

913
    pRequest->code = code;
×
914
    terrno = code;
×
915
    return pRequest->code;
32✔
916
  }
917

918
  if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_DELETE == pRequest->type ||
64,383!
919
      TDMT_VND_CREATE_TABLE == pRequest->type) {
186✔
920
    pRequest->body.resInfo.numOfRows = res.numOfRows;
64,333✔
921
    if (TDMT_VND_SUBMIT == pRequest->type) {
64,333✔
922
      STscObj*            pTscObj = pRequest->pTscObj;
64,238✔
923
      SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
64,238✔
924
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows);
64,238✔
925
    }
926

927
    schedulerFreeJob(&pRequest->body.queryJob, 0);
64,370✔
928
  }
929

930
  pRequest->code = res.code;
64,438✔
931
  terrno = res.code;
64,438✔
932
  return pRequest->code;
64,396✔
933
}
934

935
int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
1,955,392✔
936
  SArray*      pArray = NULL;
1,955,392✔
937
  SSubmitRsp2* pRsp = (SSubmitRsp2*)res;
1,955,392✔
938
  if (NULL == pRsp->aCreateTbRsp) {
1,955,392✔
939
    return TSDB_CODE_SUCCESS;
1,879,752✔
940
  }
941

942
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
75,640✔
943
  for (int32_t i = 0; i < tbNum; ++i) {
157,602✔
944
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
81,850✔
945
    if (pTbRsp->pMeta) {
81,845✔
946
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
60,456!
947
    }
948
  }
949

950
  return TSDB_CODE_SUCCESS;
75,752✔
951
}
952

953
int32_t handleQueryExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
663,375✔
954
  int32_t code = 0;
663,375✔
955
  SArray* pArray = NULL;
663,375✔
956
  SArray* pTbArray = (SArray*)res;
663,375✔
957
  int32_t tbNum = taosArrayGetSize(pTbArray);
663,375✔
958
  if (tbNum <= 0) {
663,376!
959
    return TSDB_CODE_SUCCESS;
×
960
  }
961

962
  pArray = taosArrayInit(tbNum, sizeof(STbSVersion));
663,376✔
963
  if (NULL == pArray) {
663,376!
964
    return terrno;
×
965
  }
966

967
  for (int32_t i = 0; i < tbNum; ++i) {
1,952,922✔
968
    STbVerInfo* tbInfo = taosArrayGet(pTbArray, i);
1,289,545✔
969
    if (NULL == tbInfo) {
1,289,545!
970
      code = terrno;
×
971
      goto _return;
×
972
    }
973
    STbSVersion tbSver = {
1,289,545✔
974
        .tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion, .rver = tbInfo->rversion};
1,289,545✔
975
    if (NULL == taosArrayPush(pArray, &tbSver)) {
1,289,546!
976
      code = terrno;
×
977
      goto _return;
×
978
    }
979
  }
980

981
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
663,377✔
982
                           .requestId = pRequest->requestId,
663,377✔
983
                           .requestObjRefId = pRequest->self,
663,377✔
984
                           .mgmtEps = *epset};
985

986
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
663,377✔
987

988
_return:
663,375✔
989

990
  taosArrayDestroy(pArray);
663,375✔
991
  return code;
663,378✔
992
}
993

994
int32_t handleAlterTbExecRes(void* res, SCatalog* pCatalog) {
6,407✔
995
  return catalogUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
6,407✔
996
}
997

998
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog) {
204,514✔
999
  return catalogAsyncUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
204,514✔
1000
}
1001

1002
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
2,913,648✔
1003
  if (NULL == pRequest->body.resInfo.execRes.res) {
2,913,648✔
1004
    return pRequest->code;
179,180✔
1005
  }
1006

1007
  SCatalog*     pCatalog = NULL;
2,734,468✔
1008
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
2,734,468✔
1009

1010
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
2,734,541✔
1011
  if (code) {
2,734,567!
1012
    return code;
×
1013
  }
1014

1015
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
2,734,567✔
1016
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
2,734,689✔
1017

1018
  switch (pRes->msgType) {
2,734,689✔
1019
    case TDMT_VND_ALTER_TABLE:
1,629✔
1020
    case TDMT_MND_ALTER_STB: {
1021
      code = handleAlterTbExecRes(pRes->res, pCatalog);
1,629✔
1022
      break;
1,629✔
1023
    }
1024
    case TDMT_VND_CREATE_TABLE: {
113,146✔
1025
      SArray* pList = (SArray*)pRes->res;
113,146✔
1026
      int32_t num = taosArrayGetSize(pList);
113,146✔
1027
      for (int32_t i = 0; i < num; ++i) {
249,647✔
1028
        void* res = taosArrayGetP(pList, i);
136,475✔
1029
        // handleCreateTbExecRes will handle res == null
1030
        code = handleCreateTbExecRes(res, pCatalog);
136,435✔
1031
      }
1032
      break;
113,172✔
1033
    }
1034
    case TDMT_MND_CREATE_STB: {
945✔
1035
      code = handleCreateTbExecRes(pRes->res, pCatalog);
945✔
1036
      break;
945✔
1037
    }
1038
    case TDMT_VND_SUBMIT: {
1,955,395✔
1039
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
1,955,395✔
1040

1041
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
1,955,502✔
1042
      break;
1,955,503✔
1043
    }
1044
    case TDMT_SCH_QUERY:
663,376✔
1045
    case TDMT_SCH_MERGE_QUERY: {
1046
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
663,376✔
1047
      break;
663,374✔
1048
    }
1049
    default:
198✔
1050
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self,
198!
1051
               pRequest->type, pRequest->requestId);
1052
      code = TSDB_CODE_APP_ERROR;
×
1053
  }
1054

1055
  return code;
2,734,623✔
1056
}
1057

1058
static bool incompletaFileParsing(SNode* pStmt) {
2,845,668✔
1059
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
2,845,668✔
1060
}
1061

1062
void continuePostSubQuery(SRequestObj* pRequest, SSDataBlock* pBlock) {
436✔
1063
  SSqlCallbackWrapper* pWrapper = pRequest->pWrapper;
436✔
1064

1065
  int32_t code = nodesAcquireAllocator(pWrapper->pParseCtx->allocatorId);
436✔
1066
  if (TSDB_CODE_SUCCESS == code) {
436!
1067
    int64_t analyseStart = taosGetTimestampUs();
436✔
1068
    code = qContinueParsePostQuery(pWrapper->pParseCtx, pRequest->pQuery, pBlock);
436✔
1069
    pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
436✔
1070
  }
1071

1072
  if (TSDB_CODE_SUCCESS == code) {
436!
1073
    code = qContinuePlanPostQuery(pRequest->pPostPlan);
436✔
1074
  }
1075

1076
  code = nodesReleaseAllocator(pWrapper->pParseCtx->allocatorId);
436✔
1077
  handleQueryAnslyseRes(pWrapper, NULL, code);
436✔
1078
}
436✔
1079

1080
void returnToUser(SRequestObj* pRequest) {
82,336✔
1081
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
82,336!
1082
    // return to client
1083
    doRequestCallback(pRequest, pRequest->code);
82,335✔
1084
    return;
82,335✔
1085
  }
1086

1087
  SRequestObj* pUserReq = acquireRequest(pRequest->relation.userRefId);
1✔
1088
  if (pUserReq) {
1!
1089
    pUserReq->code = pRequest->code;
1✔
1090
    // return to client
1091
    doRequestCallback(pUserReq, pUserReq->code);
1✔
1092
    (void)releaseRequest(pRequest->relation.userRefId);
1✔
1093
    return;
1✔
1094
  } else {
1095
    tscError("req:0x%" PRIx64 ", user ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1096
             pRequest->relation.userRefId, pRequest->requestId);
1097
  }
1098
}
1099

1100
static int32_t createResultBlock(TAOS_RES* pRes, int32_t numOfRows, SSDataBlock** pBlock) {
436✔
1101
  int64_t     lastTs = 0;
436✔
1102
  TAOS_FIELD* pResFields = taos_fetch_fields(pRes);
436✔
1103
  int32_t     numOfFields = taos_num_fields(pRes);
436✔
1104

1105
  int32_t code = createDataBlock(pBlock);
436✔
1106
  if (code) {
436!
1107
    return code;
×
1108
  }
1109

1110
  for (int32_t i = 0; i < numOfFields; ++i) {
1,744✔
1111
    SColumnInfoData colInfoData = createColumnInfoData(pResFields[i].type, pResFields[i].bytes, i + 1);
1,308✔
1112
    code = blockDataAppendColInfo(*pBlock, &colInfoData);
1,308✔
1113
    if (TSDB_CODE_SUCCESS != code) {
1,308!
1114
      blockDataDestroy(*pBlock);
×
1115
      return code;
×
1116
    }
1117
  }
1118

1119
  code = blockDataEnsureCapacity(*pBlock, numOfRows);
436✔
1120
  if (TSDB_CODE_SUCCESS != code) {
436!
1121
    blockDataDestroy(*pBlock);
×
1122
    return code;
×
1123
  }
1124

1125
  for (int32_t i = 0; i < numOfRows; ++i) {
1,323✔
1126
    TAOS_ROW pRow = taos_fetch_row(pRes);
887✔
1127
    if (NULL == pRow[0] || NULL == pRow[1] || NULL == pRow[2]) {
887!
1128
      tscError("invalid data from vnode");
×
1129
      blockDataDestroy(*pBlock);
×
1130
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1131
    }
1132
    int64_t ts = *(int64_t*)pRow[0];
887✔
1133
    if (lastTs < ts) {
887✔
1134
      lastTs = ts;
504✔
1135
    }
1136

1137
    for (int32_t j = 0; j < numOfFields; ++j) {
3,548✔
1138
      SColumnInfoData* pColInfoData = taosArrayGet((*pBlock)->pDataBlock, j);
2,661✔
1139
      code = colDataSetVal(pColInfoData, i, pRow[j], false);
2,661✔
1140
      if (TSDB_CODE_SUCCESS != code) {
2,661!
1141
        blockDataDestroy(*pBlock);
×
1142
        return code;
×
1143
      }
1144
    }
1145

1146
    tscInfo("[create stream with histroy] lastKey:%" PRId64 " vgId:%d, vgVer:%" PRId64, ts, *(int32_t*)pRow[1],
887!
1147
            *(int64_t*)pRow[2]);
1148
  }
1149

1150
  (*pBlock)->info.window.ekey = lastTs;
436✔
1151
  (*pBlock)->info.rows = numOfRows;
436✔
1152

1153
  tscInfo("[create stream with histroy] lastKey:%" PRId64 " numOfRows:%d from all vgroups", lastTs, numOfRows);
436!
1154
  return TSDB_CODE_SUCCESS;
436✔
1155
}
1156

1157
void postSubQueryFetchCb(void* param, TAOS_RES* res, int32_t rowNum) {
437✔
1158
  SRequestObj* pRequest = (SRequestObj*)res;
437✔
1159
  if (pRequest->code) {
437✔
1160
    returnToUser(pRequest);
1✔
1161
    return;
1✔
1162
  }
1163

1164
  SSDataBlock* pBlock = NULL;
436✔
1165
  pRequest->code = createResultBlock(res, rowNum, &pBlock);
436✔
1166
  if (TSDB_CODE_SUCCESS != pRequest->code) {
436!
1167
    tscError("req:0x%" PRIx64 ", create result block failed, QID:0x%" PRIx64 " %s", pRequest->self, pRequest->requestId,
×
1168
             tstrerror(pRequest->code));
1169
    returnToUser(pRequest);
×
1170
    return;
×
1171
  }
1172

1173
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
436✔
1174
  if (pNextReq) {
436!
1175
    continuePostSubQuery(pNextReq, pBlock);
436✔
1176
    (void)releaseRequest(pRequest->relation.nextRefId);
436✔
1177
  } else {
1178
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1179
             pRequest->relation.nextRefId, pRequest->requestId);
1180
  }
1181

1182
  blockDataDestroy(pBlock);
436✔
1183
}
1184

1185
void handlePostSubQuery(SSqlCallbackWrapper* pWrapper) {
437✔
1186
  SRequestObj* pRequest = pWrapper->pRequest;
437✔
1187
  if (TD_RES_QUERY(pRequest)) {
437!
1188
    taosAsyncFetchImpl(pRequest, postSubQueryFetchCb, pWrapper);
437✔
1189
    return;
437✔
1190
  }
1191

1192
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
×
1193
  if (pNextReq) {
×
1194
    continuePostSubQuery(pNextReq, NULL);
×
1195
    (void)releaseRequest(pRequest->relation.nextRefId);
×
1196
  } else {
1197
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1198
             pRequest->relation.nextRefId, pRequest->requestId);
1199
  }
1200
}
1201

1202
// todo refacto the error code  mgmt
1203
void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) {
2,848,399✔
1204
  SSqlCallbackWrapper* pWrapper = param;
2,848,399✔
1205
  SRequestObj*         pRequest = pWrapper->pRequest;
2,848,399✔
1206
  STscObj*             pTscObj = pRequest->pTscObj;
2,848,399✔
1207

1208
  pRequest->code = code;
2,848,399✔
1209
  if (pResult) {
2,848,399✔
1210
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
2,848,336✔
1211
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
2,848,305✔
1212
  }
1213

1214
  int32_t type = pRequest->type;
2,848,368✔
1215
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
2,848,368✔
1216
    if (pResult) {
2,023,127✔
1217
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
2,023,050✔
1218

1219
      // record the insert rows
1220
      if (TDMT_VND_SUBMIT == type) {
2,023,050✔
1221
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
1,892,015✔
1222
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
1,892,015✔
1223
      }
1224
    }
1225
    schedulerFreeJob(&pRequest->body.queryJob, 0);
2,023,167✔
1226
  }
1227

1228
  taosMemoryFree(pResult);
2,848,504!
1229
  tscDebug("req:0x%" PRIx64 ", enter scheduler exec cb, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
2,848,455✔
1230
           pRequest->requestId);
1231

1232
  if (code != TSDB_CODE_SUCCESS && NEED_CLIENT_HANDLE_ERROR(code) && pRequest->sqlstr != NULL) {
2,848,411!
1233
    tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%s, tryCount:%d, QID:0x%" PRIx64,
290✔
1234
             pRequest->self, tstrerror(code), pRequest->retry, pRequest->requestId);
1235
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
290!
1236
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1237
    }
1238
    restartAsyncQuery(pRequest, code);
290✔
1239
    return;
290✔
1240
  }
1241

1242
  tscTrace("req:0x%" PRIx64 ", scheduler exec cb, request type:%s", pRequest->self, TMSG_INFO(pRequest->type));
2,848,121!
1243
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
2,848,121!
1244
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
25,912!
1245
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1246
    }
1247
  }
1248

1249
  pRequest->metric.execCostUs = taosGetTimestampUs() - pRequest->metric.execStart;
2,848,108✔
1250
  int32_t code1 = handleQueryExecRsp(pRequest);
2,848,108✔
1251
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
2,848,196!
1252
    pRequest->code = code1;
×
1253
  }
1254

1255
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
5,693,871!
1256
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
2,845,671✔
1257
    continueInsertFromCsv(pWrapper, pRequest);
×
1258
    return;
22✔
1259
  }
1260

1261
  if (pRequest->relation.nextRefId) {
2,848,201✔
1262
    handlePostSubQuery(pWrapper);
437✔
1263
  } else {
1264
    destorySqlCallbackWrapper(pWrapper);
2,847,764✔
1265
    pRequest->pWrapper = NULL;
2,847,771✔
1266

1267
    // return to client
1268
    doRequestCallback(pRequest, code);
2,847,771✔
1269
  }
1270
}
1271

1272
void launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res) {
65,543✔
1273
  int32_t code = 0;
65,543✔
1274
  int32_t subplanNum = 0;
65,543✔
1275

1276
  if (pQuery->pRoot) {
65,543✔
1277
    pRequest->stmtType = pQuery->pRoot->type;
64,407✔
1278
  }
1279

1280
  if (pQuery->pRoot && !pRequest->inRetry) {
65,543!
1281
    STscObj*            pTscObj = pRequest->pTscObj;
64,426✔
1282
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
64,426✔
1283
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) {
64,426✔
1284
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
64,415✔
1285
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
11!
1286
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
22✔
1287
    }
1288
  }
1289

1290
  pRequest->body.execMode = pQuery->execMode;
65,604✔
1291
  switch (pQuery->execMode) {
65,604!
1292
    case QUERY_EXEC_MODE_LOCAL:
×
1293
      if (!pRequest->validateOnly) {
×
1294
        if (NULL == pQuery->pRoot) {
×
1295
          terrno = TSDB_CODE_INVALID_PARA;
×
1296
          code = terrno;
×
1297
        } else {
1298
          code = execLocalCmd(pRequest, pQuery);
×
1299
        }
1300
      }
1301
      break;
×
1302
    case QUERY_EXEC_MODE_RPC:
1,163✔
1303
      if (!pRequest->validateOnly) {
1,163!
1304
        code = execDdlQuery(pRequest, pQuery);
1,164✔
1305
      }
1306
      break;
1,165✔
1307
    case QUERY_EXEC_MODE_SCHEDULE: {
64,441✔
1308
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
64,441✔
1309
      if (NULL == pMnodeList) {
64,404!
1310
        code = terrno;
×
1311
        break;
×
1312
      }
1313
      SQueryPlan* pDag = NULL;
64,404✔
1314
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
64,404✔
1315
      if (TSDB_CODE_SUCCESS == code) {
64,391!
1316
        pRequest->body.subplanNum = pDag->numOfSubplans;
64,409✔
1317
        if (!pRequest->validateOnly) {
64,409!
1318
          SArray* pNodeList = NULL;
64,413✔
1319
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
64,413✔
1320
          if (TSDB_CODE_SUCCESS == code) {
64,423!
1321
            code = scheduleQuery(pRequest, pDag, pNodeList);
64,435✔
1322
          }
1323
          taosArrayDestroy(pNodeList);
64,377✔
1324
        }
1325
      }
1326
      taosArrayDestroy(pMnodeList);
64,439✔
1327
      break;
64,459✔
1328
    }
1329
    case QUERY_EXEC_MODE_EMPTY_RESULT:
×
1330
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
1331
      break;
×
1332
    default:
×
1333
      break;
×
1334
  }
1335

1336
  if (!keepQuery) {
65,624!
1337
    qDestroyQuery(pQuery);
×
1338
  }
1339

1340
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
65,624!
1341
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
223!
1342
    if (TSDB_CODE_SUCCESS != ret) {
223!
1343
      tscError("req:0x%" PRIx64 ", remove meta failed,code:%d, QID:0x%" PRIx64, pRequest->self, ret,
×
1344
               pRequest->requestId);
1345
    }
1346
  }
1347

1348
  if (TSDB_CODE_SUCCESS == code) {
65,624✔
1349
    code = handleQueryExecRsp(pRequest);
65,609✔
1350
  }
1351

1352
  if (TSDB_CODE_SUCCESS != code) {
65,622✔
1353
    pRequest->code = code;
139✔
1354
  }
1355

1356
  if (res) {
65,622!
1357
    *res = pRequest->body.resInfo.execRes.res;
×
1358
    pRequest->body.resInfo.execRes.res = NULL;
×
1359
  }
1360
}
65,622✔
1361

1362
static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta,
2,849,453✔
1363
                                 SSqlCallbackWrapper* pWrapper) {
1364
  int32_t code = TSDB_CODE_SUCCESS;
2,849,453✔
1365
  pRequest->type = pQuery->msgType;
2,849,453✔
1366
  SArray*     pMnodeList = NULL;
2,849,453✔
1367
  SQueryPlan* pDag = NULL;
2,849,453✔
1368
  int64_t     st = taosGetTimestampUs();
2,849,572✔
1369

1370
  if (!pRequest->parseOnly) {
2,849,572!
1371
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
2,849,599✔
1372
    if (NULL == pMnodeList) {
2,849,506!
1373
      code = terrno;
×
1374
    }
1375
    SPlanContext cxt = {.queryId = pRequest->requestId,
5,699,175✔
1376
                        .acctId = pRequest->pTscObj->acctId,
2,849,506✔
1377
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
2,849,506✔
1378
                        .pAstRoot = pQuery->pRoot,
2,849,669✔
1379
                        .showRewrite = pQuery->showRewrite,
2,849,669✔
1380
                        .isView = pWrapper->pParseCtx->isView,
2,849,669✔
1381
                        .isAudit = pWrapper->pParseCtx->isAudit,
2,849,669✔
1382
                        .pMsg = pRequest->msgBuf,
2,849,669✔
1383
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1384
                        .pUser = pRequest->pTscObj->user,
2,849,669✔
1385
                        .sysInfo = pRequest->pTscObj->sysInfo,
2,849,669✔
1386
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
2,849,669✔
1387
                        .allocatorId = pRequest->allocatorRefId};
2,849,669✔
1388
    if (TSDB_CODE_SUCCESS == code) {
2,849,669✔
1389
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
2,849,628✔
1390
    }
1391
    if (code) {
2,849,243✔
1392
      tscError("req:0x%" PRIx64 ", failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
674!
1393
               pRequest->requestId);
1394
    } else {
1395
      pRequest->body.subplanNum = pDag->numOfSubplans;
2,848,569✔
1396
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
2,848,569✔
1397
    }
1398
  }
1399

1400
  pRequest->metric.execStart = taosGetTimestampUs();
2,849,327✔
1401
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
2,849,327✔
1402

1403
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
5,697,689✔
1404
    SArray* pNodeList = NULL;
2,848,337✔
1405
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
2,848,337✔
1406
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
811,784✔
1407
    }
1408

1409
    SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
2,848,343✔
1410
                             .requestId = pRequest->requestId,
2,848,157✔
1411
                             .requestObjRefId = pRequest->self};
2,848,157✔
1412
    SSchedulerReq    req = {
5,696,241✔
1413
           .syncReq = false,
1414
           .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
2,848,157✔
1415
           .pConn = &conn,
1416
           .pNodeList = pNodeList,
1417
           .pDag = pDag,
1418
           .allocatorRefId = pRequest->allocatorRefId,
2,848,157✔
1419
           .sql = pRequest->sqlstr,
2,848,157✔
1420
           .startTs = pRequest->metric.start,
2,848,157✔
1421
           .execFp = schedulerExecCb,
1422
           .cbParam = pWrapper,
1423
           .chkKillFp = chkRequestKilled,
1424
           .chkKillParam = (void*)pRequest->self,
2,848,157✔
1425
           .pExecRes = NULL,
1426
           .source = pRequest->source,
2,848,157✔
1427
           .pWorkerCb = getTaskPoolWorkerCb(),
2,848,157✔
1428
    };
1429
    if (TSDB_CODE_SUCCESS == code) {
2,848,084!
1430
      code = schedulerExecJob(&req, &pRequest->body.queryJob);
2,848,255✔
1431
    }
1432

1433
    taosArrayDestroy(pNodeList);
2,848,230✔
1434
  } else {
1435
    qDestroyQueryPlan(pDag);
990✔
1436
    tscDebug("req:0x%" PRIx64 ", plan not executed, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
1,138✔
1437
             pRequest->requestId);
1438
    destorySqlCallbackWrapper(pWrapper);
1,138✔
1439
    pRequest->pWrapper = NULL;
1,138✔
1440
    if (TSDB_CODE_SUCCESS != code) {
1,138✔
1441
      pRequest->code = terrno;
674✔
1442
    }
1443

1444
    doRequestCallback(pRequest, code);
1,138✔
1445
  }
1446

1447
  // todo not to be released here
1448
  taosArrayDestroy(pMnodeList);
2,849,500✔
1449

1450
  return code;
2,849,537✔
1451
}
1452

1453
void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta, SSqlCallbackWrapper* pWrapper) {
3,020,568✔
1454
  int32_t code = 0;
3,020,568✔
1455

1456
  if (pRequest->parseOnly) {
3,020,568✔
1457
    doRequestCallback(pRequest, 0);
640✔
1458
    return;
640✔
1459
  }
1460

1461
  pRequest->body.execMode = pQuery->execMode;
3,019,928✔
1462
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
3,019,928✔
1463
    destorySqlCallbackWrapper(pWrapper);
170,750✔
1464
    pRequest->pWrapper = NULL;
170,753✔
1465
  }
1466

1467
  if (pQuery->pRoot && !pRequest->inRetry) {
3,019,931!
1468
    STscObj*            pTscObj = pRequest->pTscObj;
3,020,261✔
1469
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
3,020,261✔
1470
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
3,020,261✔
1471
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
2,036,698✔
1472
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
1,891,989✔
1473
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
1,128,272✔
1474
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
754,791✔
1475
    }
1476
  }
1477

1478
  switch (pQuery->execMode) {
3,020,406!
1479
    case QUERY_EXEC_MODE_LOCAL:
125,905✔
1480
      asyncExecLocalCmd(pRequest, pQuery);
125,905✔
1481
      break;
125,905✔
1482
    case QUERY_EXEC_MODE_RPC:
42,137✔
1483
      code = asyncExecDdlQuery(pRequest, pQuery);
42,137✔
1484
      break;
42,147✔
1485
    case QUERY_EXEC_MODE_SCHEDULE: {
2,849,642✔
1486
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
2,849,642✔
1487
      break;
2,849,483✔
1488
    }
1489
    case QUERY_EXEC_MODE_EMPTY_RESULT:
2,722✔
1490
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
2,722✔
1491
      doRequestCallback(pRequest, 0);
2,722✔
1492
      break;
2,722✔
1493
    default:
×
1494
      tscError("req:0x%" PRIx64 ", invalid execMode %d", pRequest->self, pQuery->execMode);
×
1495
      doRequestCallback(pRequest, -1);
×
1496
      break;
×
1497
  }
1498
}
1499

1500
int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) {
37✔
1501
  SCatalog* pCatalog = NULL;
37✔
1502
  int32_t   code = 0;
37✔
1503
  int32_t   dbNum = taosArrayGetSize(pRequest->dbList);
37✔
1504
  int32_t   tblNum = taosArrayGetSize(pRequest->tableList);
37✔
1505

1506
  if (dbNum <= 0 && tblNum <= 0) {
37!
1507
    return TSDB_CODE_APP_ERROR;
37✔
1508
  }
1509

1510
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
×
1511
  if (code != TSDB_CODE_SUCCESS) {
×
1512
    return code;
×
1513
  }
1514

1515
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
×
1516
                           .requestId = pRequest->requestId,
×
1517
                           .requestObjRefId = pRequest->self,
×
1518
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
×
1519

1520
  for (int32_t i = 0; i < dbNum; ++i) {
×
1521
    char* dbFName = taosArrayGet(pRequest->dbList, i);
×
1522

1523
    // catalogRefreshDBVgInfo will handle dbFName == null.
1524
    code = catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
×
1525
    if (code != TSDB_CODE_SUCCESS) {
×
1526
      return code;
×
1527
    }
1528
  }
1529

1530
  for (int32_t i = 0; i < tblNum; ++i) {
×
1531
    SName* tableName = taosArrayGet(pRequest->tableList, i);
×
1532

1533
    // catalogRefreshTableMeta will handle tableName == null.
1534
    code = catalogRefreshTableMeta(pCatalog, &conn, tableName, -1);
×
1535
    if (code != TSDB_CODE_SUCCESS) {
×
1536
      return code;
×
1537
    }
1538
  }
1539

1540
  return code;
×
1541
}
1542

1543
int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView) {
29,577✔
1544
  SCatalog* pCatalog = NULL;
29,577✔
1545
  int32_t   tbNum = taosArrayGetSize(tbList);
29,577✔
1546
  int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
29,577✔
1547
  if (code != TSDB_CODE_SUCCESS) {
29,577!
1548
    return code;
×
1549
  }
1550

1551
  if (isView) {
29,577✔
1552
    for (int32_t i = 0; i < tbNum; ++i) {
1,758✔
1553
      SName* pViewName = taosArrayGet(tbList, i);
879✔
1554
      char   dbFName[TSDB_DB_FNAME_LEN];
1555
      if (NULL == pViewName) {
879!
1556
        continue;
×
1557
      }
1558
      (void)tNameGetFullDbName(pViewName, dbFName);
879✔
1559
      TSC_ERR_RET(catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0));
879!
1560
    }
1561
  } else {
1562
    for (int32_t i = 0; i < tbNum; ++i) {
47,316✔
1563
      SName* pTbName = taosArrayGet(tbList, i);
18,618✔
1564
      TSC_ERR_RET(catalogRemoveTableMeta(pCatalog, pTbName));
18,618!
1565
    }
1566
  }
1567

1568
  return TSDB_CODE_SUCCESS;
29,577✔
1569
}
1570

1571
int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) {
31,071✔
1572
  pEpSet->version = 0;
31,071✔
1573

1574
  // init mnode ip set
1575
  SEpSet* mgmtEpSet = &(pEpSet->epSet);
31,071✔
1576
  mgmtEpSet->numOfEps = 0;
31,071✔
1577
  mgmtEpSet->inUse = 0;
31,071✔
1578

1579
  if (firstEp && firstEp[0] != 0) {
31,071!
1580
    if (strlen(firstEp) >= TSDB_EP_LEN) {
31,166!
1581
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1582
      return -1;
×
1583
    }
1584

1585
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
31,166✔
1586
    if (code != TSDB_CODE_SUCCESS) {
31,145!
1587
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1588
      return terrno;
×
1589
    }
1590
    // uint32_t addr = 0;
1591
    SIpAddr addr = {0};
31,145✔
1592
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
31,145✔
1593
    if (code) {
31,108!
1594
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
×
1595
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1596
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
4✔
1597
    } else {
1598
      mgmtEpSet->numOfEps++;
31,123✔
1599
    }
1600
  }
1601

1602
  if (secondEp && secondEp[0] != 0) {
31,032!
1603
    if (strlen(secondEp) >= TSDB_EP_LEN) {
21,461!
1604
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1605
      return terrno;
×
1606
    }
1607

1608
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
21,461✔
1609
    if (code != TSDB_CODE_SUCCESS) {
21,461!
1610
      return code;
×
1611
    }
1612
    SIpAddr addr = {0};
21,461✔
1613
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
21,461✔
1614
    if (code) {
21,460!
1615
      tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
×
1616
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1617
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
×
1618
    } else {
1619
      mgmtEpSet->numOfEps++;
21,460✔
1620
    }
1621
  }
1622

1623
  if (mgmtEpSet->numOfEps == 0) {
31,031✔
1624
    terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
4✔
1625
    return TSDB_CODE_RPC_NETWORK_UNAVAIL;
4✔
1626
  }
1627

1628
  return 0;
31,027✔
1629
}
1630

1631
int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param,
31,189✔
1632
                        SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj) {
1633
  *pTscObj = NULL;
31,189✔
1634
  int32_t code = createTscObj(user, auth, db, connType, pAppInfo, pTscObj);
31,189✔
1635
  if (TSDB_CODE_SUCCESS != code) {
31,190!
1636
    return code;
×
1637
  }
1638

1639
  SRequestObj* pRequest = NULL;
31,190✔
1640
  code = createRequest((*pTscObj)->id, TDMT_MND_CONNECT, 0, &pRequest);
31,190✔
1641
  if (TSDB_CODE_SUCCESS != code) {
31,179!
1642
    destroyTscObj(*pTscObj);
×
1643
    return code;
×
1644
  }
1645

1646
  pRequest->sqlstr = taosStrdup("taos_connect");
31,179!
1647
  if (pRequest->sqlstr) {
31,172!
1648
    pRequest->sqlLen = strlen(pRequest->sqlstr);
31,172✔
1649
  } else {
1650
    return terrno;
×
1651
  }
1652

1653
  SMsgSendInfo* body = NULL;
31,172✔
1654
  code = buildConnectMsg(pRequest, &body);
31,172✔
1655
  if (TSDB_CODE_SUCCESS != code) {
31,141!
1656
    destroyTscObj(*pTscObj);
×
1657
    return code;
×
1658
  }
1659

1660
  // int64_t transporterId = 0;
1661
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &(*pTscObj)->pAppInfo->mgmtEp.epSet, NULL, body);
31,141✔
1662
  if (TSDB_CODE_SUCCESS != code) {
31,187!
1663
    destroyTscObj(*pTscObj);
×
1664
    tscError("failed to send connect msg to server, code:%s", tstrerror(code));
×
1665
    return code;
×
1666
  }
1667
  if (TSDB_CODE_SUCCESS != tsem_wait(&pRequest->body.rspSem)) {
31,187!
1668
    destroyTscObj(*pTscObj);
×
1669
    tscError("failed to wait sem, code:%s", terrstr());
×
1670
    return terrno;
×
1671
  }
1672
  if (pRequest->code != TSDB_CODE_SUCCESS) {
31,185✔
1673
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
66!
1674
    tscError("failed to connect to server, reason: %s", errorMsg);
66!
1675

1676
    terrno = pRequest->code;
66✔
1677
    destroyRequest(pRequest);
66✔
1678
    taos_close_internal(*pTscObj);
66✔
1679
    *pTscObj = NULL;
66✔
1680
    return terrno;
66✔
1681
  } else {
1682
    tscInfo("conn:0x%" PRIx64 ", connection is opening, connId:%u, dnodeConn:%p, QID:0x%" PRIx64, (*pTscObj)->id,
31,119!
1683
            (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId);
1684
    destroyRequest(pRequest);
31,122✔
1685
  }
1686
  return code;
31,117✔
1687
}
1688

1689
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo) {
31,162✔
1690
  *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
31,162!
1691
  if (*pMsgSendInfo == NULL) {
31,180!
1692
    return terrno;
×
1693
  }
1694

1695
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
31,180✔
1696

1697
  (*pMsgSendInfo)->requestObjRefId = pRequest->self;
31,180✔
1698
  (*pMsgSendInfo)->requestId = pRequest->requestId;
31,180✔
1699
  (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType);
31,180✔
1700
  (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self));
31,171!
1701
  if (NULL == (*pMsgSendInfo)->param) {
31,167!
1702
    taosMemoryFree(*pMsgSendInfo);
×
1703
    return terrno;
×
1704
  }
1705

1706
  *(int64_t*)(*pMsgSendInfo)->param = pRequest->self;
31,167✔
1707

1708
  SConnectReq connectReq = {0};
31,167✔
1709
  STscObj*    pObj = pRequest->pTscObj;
31,167✔
1710

1711
  char* db = getDbOfConnection(pObj);
31,167✔
1712
  if (db != NULL) {
31,191✔
1713
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
1,547✔
1714
  } else if (terrno) {
29,644!
1715
    taosMemoryFree(*pMsgSendInfo);
×
1716
    return terrno;
×
1717
  }
1718
  taosMemoryFreeClear(db);
31,182!
1719

1720
  connectReq.connType = pObj->connType;
31,184✔
1721
  connectReq.pid = appInfo.pid;
31,184✔
1722
  connectReq.startTime = appInfo.startTime;
31,184✔
1723

1724
  tstrncpy(connectReq.app, appInfo.appName, sizeof(connectReq.app));
31,184✔
1725
  tstrncpy(connectReq.user, pObj->user, sizeof(connectReq.user));
31,184✔
1726
  tstrncpy(connectReq.passwd, pObj->pass, sizeof(connectReq.passwd));
31,184✔
1727
  tstrncpy(connectReq.sVer, td_version, sizeof(connectReq.sVer));
31,184✔
1728

1729
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
31,184✔
1730
  void*   pReq = taosMemoryMalloc(contLen);
31,154!
1731
  if (NULL == pReq) {
31,163!
1732
    taosMemoryFree(*pMsgSendInfo);
×
1733
    return terrno;
×
1734
  }
1735

1736
  if (-1 == tSerializeSConnectReq(pReq, contLen, &connectReq)) {
31,163✔
1737
    taosMemoryFree(*pMsgSendInfo);
16!
1738
    taosMemoryFree(pReq);
×
1739
    return terrno;
×
1740
  }
1741

1742
  (*pMsgSendInfo)->msgInfo.len = contLen;
31,146✔
1743
  (*pMsgSendInfo)->msgInfo.pData = pReq;
31,146✔
1744
  return TSDB_CODE_SUCCESS;
31,146✔
1745
}
1746

1747
void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, SEpSet* pEpSet) {
6,448,408✔
1748
  if (NULL == pEpSet) {
6,448,408✔
1749
    return;
4,995,178✔
1750
  }
1751

1752
  switch (pSendInfo->target.type) {
1,453,230✔
1753
    case TARGET_TYPE_MNODE:
21✔
1754
      if (NULL == pTscObj) {
21!
1755
        tscError("mnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1756
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1757
        return;
×
1758
      }
1759

1760
      SEpSet* pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
21✔
1761
      SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
21✔
1762
      SEp*    pNewEp = &pEpSet->eps[pEpSet->inUse];
21✔
1763
      tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in client", pOrig->inUse, pOrig->numOfEps,
21!
1764
               pOrigEp->fqdn, pOrigEp->port, pEpSet->inUse, pEpSet->numOfEps, pNewEp->fqdn, pNewEp->port);
1765
      updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
21✔
1766
      break;
21✔
1767
    case TARGET_TYPE_VNODE: {
828,369✔
1768
      if (NULL == pTscObj) {
828,369!
1769
        tscError("vnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1770
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1771
        return;
×
1772
      }
1773

1774
      SCatalog* pCatalog = NULL;
828,369✔
1775
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
828,369✔
1776
      if (code != TSDB_CODE_SUCCESS) {
828,350!
1777
        tscError("fail to get catalog handle, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1778
                 tstrerror(code));
1779
        return;
×
1780
      }
1781

1782
      code = catalogUpdateVgEpSet(pCatalog, pSendInfo->target.dbFName, pSendInfo->target.vgId, pEpSet);
828,350✔
1783
      if (code != TSDB_CODE_SUCCESS) {
828,373!
1784
        tscError("fail to update catalog vg epset, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1785
                 tstrerror(code));
1786
        return;
×
1787
      }
1788
      taosMemoryFreeClear(pSendInfo->target.dbFName);
828,373!
1789
      break;
828,374✔
1790
    }
1791
    default:
624,840✔
1792
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
624,840!
1793
      break;
624,870✔
1794
  }
1795
}
1796

1797
int32_t doProcessMsgFromServerImpl(SRpcMsg* pMsg, SEpSet* pEpSet) {
6,448,930✔
1798
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
6,448,930✔
1799
  if (pMsg->info.ahandle == NULL) {
6,448,930✔
1800
    tscError("doProcessMsgFromServer pMsg->info.ahandle == NULL");
428!
1801
    rpcFreeCont(pMsg->pCont);
428✔
1802
    taosMemoryFree(pEpSet);
428!
1803
    return TSDB_CODE_TSC_INTERNAL_ERROR;
428✔
1804
  }
1805

1806
  STscObj* pTscObj = NULL;
6,448,502✔
1807

1808
  STraceId* trace = &pMsg->info.traceId;
6,448,502✔
1809
  char      tbuf[40] = {0};
6,448,502✔
1810
  TRACE_TO_STR(trace, tbuf);
6,448,502!
1811

1812
  tscDebug("QID:%s, process message from server, handle:%p, message:%s, size:%d, code:%s", tbuf, pMsg->info.handle,
6,448,380!
1813
           TMSG_INFO(pMsg->msgType), pMsg->contLen, tstrerror(pMsg->code));
1814

1815
  if (pSendInfo->requestObjRefId != 0) {
6,448,548✔
1816
    SRequestObj* pRequest = (SRequestObj*)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
5,076,717✔
1817
    if (pRequest) {
5,076,695✔
1818
      if (pRequest->self != pSendInfo->requestObjRefId) {
5,071,927!
1819
        tscError("doProcessMsgFromServer req:0x%" PRId64 " != pSendInfo->requestObjRefId:0x%" PRId64, pRequest->self,
×
1820
                 pSendInfo->requestObjRefId);
1821

1822
        if (TSDB_CODE_SUCCESS != taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId)) {
×
1823
          tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1824
        }
1825
        rpcFreeCont(pMsg->pCont);
×
1826
        taosMemoryFree(pEpSet);
×
1827
        destroySendMsgInfo(pSendInfo);
×
1828
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1829
      }
1830
      pTscObj = pRequest->pTscObj;
5,071,927✔
1831
    }
1832
  }
1833

1834
  updateTargetEpSet(pSendInfo, pTscObj, pMsg, pEpSet);
6,448,526✔
1835

1836
  SDataBuf buf = {.msgType = pMsg->msgType,
6,448,393✔
1837
                  .len = pMsg->contLen,
6,448,393✔
1838
                  .pData = NULL,
1839
                  .handle = pMsg->info.handle,
6,448,393✔
1840
                  .handleRefId = pMsg->info.refId,
6,448,393✔
1841
                  .pEpSet = pEpSet};
1842

1843
  if (pMsg->contLen > 0) {
6,448,393✔
1844
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
6,369,945!
1845
    if (buf.pData == NULL) {
6,369,938!
1846
      pMsg->code = terrno;
×
1847
    } else {
1848
      (void)memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
6,369,938✔
1849
    }
1850
  }
1851

1852
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
6,448,386✔
1853

1854
  if (pTscObj) {
6,448,395✔
1855
    int32_t code = taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
5,071,857✔
1856
    if (TSDB_CODE_SUCCESS != code) {
5,071,935!
1857
      tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1858
      terrno = code;
×
1859
      pMsg->code = code;
×
1860
    }
1861
  }
1862

1863
  rpcFreeCont(pMsg->pCont);
6,448,473✔
1864
  destroySendMsgInfo(pSendInfo);
6,448,495✔
1865
  return TSDB_CODE_SUCCESS;
6,448,504✔
1866
}
1867

1868
int32_t doProcessMsgFromServer(void* param) {
6,448,994✔
1869
  AsyncArg* arg = (AsyncArg*)param;
6,448,994✔
1870
  int32_t   code = doProcessMsgFromServerImpl(&arg->msg, arg->pEpset);
6,448,994✔
1871
  taosMemoryFree(arg);
6,448,913!
1872
  return code;
6,448,919✔
1873
}
1874

1875
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
6,448,181✔
1876
  int32_t code = 0;
6,448,181✔
1877
  SEpSet* tEpSet = NULL;
6,448,181✔
1878

1879
  tscDebug("msg callback, ahandle %p", pMsg->info.ahandle);
6,448,181✔
1880

1881
  if (pEpSet != NULL) {
6,448,224✔
1882
    tEpSet = taosMemoryCalloc(1, sizeof(SEpSet));
1,453,259!
1883
    if (NULL == tEpSet) {
1,453,254!
1884
      code = terrno;
×
1885
      pMsg->code = terrno;
×
1886
      goto _exit;
×
1887
    }
1888
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
1,453,254✔
1889
  }
1890

1891
  // pMsg is response msg
1892
  if (pMsg->msgType == TDMT_MND_CONNECT + 1) {
6,448,219✔
1893
    // restore origin code
1894
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
31,175!
1895
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
1896
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
31,175!
1897
      pMsg->code = TSDB_CODE_RPC_BROKEN_LINK;
×
1898
    }
1899
  } else {
1900
    // uniform to one error code: TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED
1901
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
6,417,044!
1902
      pMsg->code = TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED;
×
1903
    }
1904
  }
1905

1906
  AsyncArg* arg = taosMemoryCalloc(1, sizeof(AsyncArg));
6,448,219!
1907
  if (NULL == arg) {
6,448,626!
1908
    code = terrno;
×
1909
    pMsg->code = code;
×
1910
    goto _exit;
×
1911
  }
1912

1913
  arg->msg = *pMsg;
6,448,626✔
1914
  arg->pEpset = tEpSet;
6,448,626✔
1915

1916
  if ((code = taosAsyncExec(doProcessMsgFromServer, arg, NULL)) != 0) {
6,448,626✔
1917
    pMsg->code = code;
100✔
1918
    taosMemoryFree(arg);
100!
1919
    goto _exit;
×
1920
  }
1921
  return;
6,448,763✔
1922

1923
_exit:
×
1924
  tscError("failed to sched msg to tsc since %s", tstrerror(code));
×
1925
  code = doProcessMsgFromServerImpl(pMsg, tEpSet);
×
1926
  if (code != 0) {
×
1927
    tscError("failed to sched msg to tsc, tsc ready quit");
×
1928
  }
1929
}
1930

1931
TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, const char* db, uint16_t port) {
4✔
1932
  tscInfo("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
4!
1933
  if (user == NULL) {
4!
1934
    user = TSDB_DEFAULT_USER;
×
1935
  }
1936

1937
  if (auth == NULL) {
4!
1938
    tscError("No auth info is given, failed to connect to server");
×
1939
    return NULL;
×
1940
  }
1941

1942
  STscObj* pObj = NULL;
4✔
1943
  int32_t  code = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY, &pObj);
4✔
1944
  if (TSDB_CODE_SUCCESS == code) {
4✔
1945
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1!
1946
    if (NULL == rid) {
1!
1947
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
1948
    }
1949
    *rid = pObj->id;
1✔
1950
    return (TAOS*)rid;
1✔
1951
  }
1952

1953
  return NULL;
3✔
1954
}
1955

1956
// TAOS* taos_connect_l(const char* ip, int ipLen, const char* user, int userLen, const char* pass, int passLen,
1957
//                      const char* db, int dbLen, uint16_t port) {
1958
//   char ipStr[TSDB_EP_LEN] = {0};
1959
//   char dbStr[TSDB_DB_NAME_LEN] = {0};
1960
//   char userStr[TSDB_USER_LEN] = {0};
1961
//   char passStr[TSDB_PASSWORD_LEN] = {0};
1962
//
1963
//   tstrncpy(ipStr, ip, TMIN(TSDB_EP_LEN - 1, ipLen));
1964
//   tstrncpy(userStr, user, TMIN(TSDB_USER_LEN - 1, userLen));
1965
//   tstrncpy(passStr, pass, TMIN(TSDB_PASSWORD_LEN - 1, passLen));
1966
//   tstrncpy(dbStr, db, TMIN(TSDB_DB_NAME_LEN - 1, dbLen));
1967
//   return taos_connect(ipStr, userStr, passStr, dbStr, port);
1968
// }
1969

1970
void doSetOneRowPtr(SReqResultInfo* pResultInfo) {
35,551,997✔
1971
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
176,211,398✔
1972
    SResultColumn* pCol = &pResultInfo->pCol[i];
140,742,848✔
1973

1974
    int32_t type = pResultInfo->fields[i].type;
140,742,848✔
1975
    int32_t schemaBytes = calcSchemaBytesFromTypeBytes(type, pResultInfo->userFields[i].bytes, false);
140,742,848✔
1976

1977
    if (IS_VAR_DATA_TYPE(type)) {
140,659,401✔
1978
      if (!IS_VAR_NULL_TYPE(type, schemaBytes) && pCol->offset[pResultInfo->current] != -1) {
27,453,241!
1979
        char* pStart = pResultInfo->pCol[i].offset[pResultInfo->current] + pResultInfo->pCol[i].pData;
26,036,991✔
1980

1981
        pResultInfo->length[i] = varDataLen(pStart);
26,036,991✔
1982
        pResultInfo->row[i] = varDataVal(pStart);
26,036,991✔
1983
      } else {
1984
        pResultInfo->row[i] = NULL;
1,416,250✔
1985
        pResultInfo->length[i] = 0;
1,416,250✔
1986
      }
1987
    } else {
1988
      if (!colDataIsNull_f(pCol->nullbitmap, pResultInfo->current)) {
113,206,160✔
1989
        pResultInfo->row[i] = pResultInfo->pCol[i].pData + schemaBytes * pResultInfo->current;
107,794,077✔
1990
        pResultInfo->length[i] = schemaBytes;
107,794,077✔
1991
      } else {
1992
        pResultInfo->row[i] = NULL;
5,412,083✔
1993
        pResultInfo->length[i] = 0;
5,412,083✔
1994
      }
1995
    }
1996
  }
1997
}
35,468,550✔
1998

1999
void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
×
2000
  if (pRequest == NULL) {
×
2001
    return NULL;
×
2002
  }
2003

2004
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
×
2005
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
×
2006
    // All data has returned to App already, no need to try again
2007
    if (pResultInfo->completed) {
×
2008
      pResultInfo->numOfRows = 0;
×
2009
      return NULL;
×
2010
    }
2011

2012
    SReqResultInfo* pResInfo = &pRequest->body.resInfo;
×
2013
    SSchedulerReq   req = {.syncReq = true, .pFetchRes = (void**)&pResInfo->pData};
×
2014

2015
    pRequest->code = schedulerFetchRows(pRequest->body.queryJob, &req);
×
2016
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2017
      pResultInfo->numOfRows = 0;
×
2018
      return NULL;
×
2019
    }
2020

2021
    pRequest->code = setQueryResultFromRsp(&pRequest->body.resInfo, (const SRetrieveTableRsp*)pResInfo->pData,
×
2022
                                           convertUcs4, pRequest->isStmtBind);
×
2023
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2024
      pResultInfo->numOfRows = 0;
×
2025
      return NULL;
×
2026
    }
2027

2028
    tscDebug("req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64
×
2029
             ", complete:%d, QID:0x%" PRIx64,
2030
             pRequest->self, pResInfo->numOfRows, pResInfo->totalRows, pResInfo->completed, pRequest->requestId);
2031

2032
    STscObj*            pTscObj = pRequest->pTscObj;
×
2033
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
×
2034
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
×
2035

2036
    if (pResultInfo->numOfRows == 0) {
×
2037
      return NULL;
×
2038
    }
2039
  }
2040

2041
  if (setupOneRowPtr) {
×
2042
    doSetOneRowPtr(pResultInfo);
×
2043
    pResultInfo->current += 1;
×
2044
  }
2045

2046
  return pResultInfo->row;
×
2047
}
2048

2049
static void syncFetchFn(void* param, TAOS_RES* res, int32_t numOfRows) {
854,162✔
2050
  tsem_t* sem = param;
854,162✔
2051
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
854,162!
2052
    tscError("failed to post sem, code:%s", terrstr());
×
2053
  }
2054
}
854,162✔
2055

2056
void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
20,559,252✔
2057
  if (pRequest == NULL) {
20,559,252!
2058
    return NULL;
×
2059
  }
2060

2061
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
20,559,252✔
2062
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
20,559,252✔
2063
    // All data has returned to App already, no need to try again
2064
    if (pResultInfo->completed) {
1,563,451✔
2065
      pResultInfo->numOfRows = 0;
709,289✔
2066
      return NULL;
709,289✔
2067
    }
2068

2069
    // convert ucs4 to native multi-bytes string
2070
    pResultInfo->convertUcs4 = convertUcs4;
854,162✔
2071
    tsem_t sem;
2072
    if (TSDB_CODE_SUCCESS != tsem_init(&sem, 0, 0)) {
854,162!
2073
      tscError("failed to init sem, code:%s", terrstr());
×
2074
    }
2075
    taos_fetch_rows_a(pRequest, syncFetchFn, &sem);
854,159✔
2076
    if (TSDB_CODE_SUCCESS != tsem_wait(&sem)) {
854,162!
2077
      tscError("failed to wait sem, code:%s", terrstr());
×
2078
    }
2079
    if (TSDB_CODE_SUCCESS != tsem_destroy(&sem)) {
854,158!
2080
      tscError("failed to destroy sem, code:%s", terrstr());
×
2081
    }
2082
    pRequest->inCallback = false;
854,160✔
2083
  }
2084

2085
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
19,849,961!
2086
    return NULL;
64,574✔
2087
  } else {
2088
    if (setupOneRowPtr) {
19,785,387✔
2089
      doSetOneRowPtr(pResultInfo);
18,933,723✔
2090
      pResultInfo->current += 1;
18,924,581✔
2091
    }
2092

2093
    return pResultInfo->row;
19,776,245✔
2094
  }
2095
}
2096

2097
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
1,348,972✔
2098
  if (pResInfo->row == NULL) {
1,348,972✔
2099
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
1,152,142!
2100
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
1,152,152!
2101
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
1,152,148!
2102
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
1,152,158!
2103

2104
    if (pResInfo->row == NULL || pResInfo->pCol == NULL || pResInfo->length == NULL || pResInfo->convertBuf == NULL) {
1,152,154!
UNCOV
2105
      taosMemoryFree(pResInfo->row);
×
2106
      taosMemoryFree(pResInfo->pCol);
×
2107
      taosMemoryFree(pResInfo->length);
×
2108
      taosMemoryFree(pResInfo->convertBuf);
×
2109
      return terrno;
×
2110
    }
2111
  }
2112

2113
  return TSDB_CODE_SUCCESS;
1,348,984✔
2114
}
2115

2116
static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength, bool isStmt) {
1,348,670✔
2117
  int32_t idx = -1;
1,348,670✔
2118
  iconv_t conv = taosAcquireConv(&idx, C2M, pResultInfo->charsetCxt);
1,348,670✔
2119
  if (conv == (iconv_t)-1) return TSDB_CODE_TSC_INTERNAL_ERROR;
1,348,716!
2120

2121
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,763,413✔
2122
    int32_t type = pResultInfo->fields[i].type;
5,414,726✔
2123
    int32_t schemaBytes =
2124
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
5,414,726✔
2125

2126
    if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) {
5,414,728✔
2127
      char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]);
249,841!
2128
      if (p == NULL) {
249,841!
2129
        taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2130
        return terrno;
×
2131
      }
2132

2133
      pResultInfo->convertBuf[i] = p;
249,841✔
2134

2135
      SResultColumn* pCol = &pResultInfo->pCol[i];
249,841✔
2136
      for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
39,299,589✔
2137
        if (pCol->offset[j] != -1) {
39,049,779✔
2138
          char* pStart = pCol->offset[j] + pCol->pData;
33,519,627✔
2139

2140
          int32_t len = taosUcs4ToMbsEx((TdUcs4*)varDataVal(pStart), varDataLen(pStart), varDataVal(p), conv);
33,519,627✔
2141
          if (len < 0 || len > schemaBytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) {
33,519,616!
2142
            tscError(
20✔
2143
                "doConvertUCS4 error, invalid data. len:%d, bytes:%d, (p + len):%p, (pResultInfo->convertBuf[i] + "
2144
                "colLength[i]):%p",
2145
                len, schemaBytes, (p + len), (pResultInfo->convertBuf[i] + colLength[i]));
2146
            taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
20✔
2147
            return TSDB_CODE_TSC_INTERNAL_ERROR;
1✔
2148
          }
2149

2150
          varDataSetLen(p, len);
33,519,596✔
2151
          pCol->offset[j] = (p - pResultInfo->convertBuf[i]);
33,519,596✔
2152
          p += (len + VARSTR_HEADER_SIZE);
33,519,596✔
2153
        }
2154
      }
2155

2156
      pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
249,810✔
2157
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
249,810✔
2158
    }
2159
  }
2160
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
1,348,687✔
2161
  return TSDB_CODE_SUCCESS;
1,348,727✔
2162
}
2163

2164
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
1,348,723✔
2165
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,763,842✔
2166
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
5,415,119✔
2167
    TAOS_FIELD*   pField = pResultInfo->userFields + i;
5,415,119✔
2168
    int32_t       type = pFieldE->type;
5,415,119✔
2169
    int32_t       bufLen = 0;
5,415,119✔
2170
    char*         p = NULL;
5,415,119✔
2171
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
5,415,119✔
2172
      continue;
5,407,091✔
2173
    } else {
2174
      bufLen = 64;
8,028✔
2175
      p = taosMemoryRealloc(pResultInfo->convertBuf[i], bufLen * pResultInfo->numOfRows);
8,028!
2176
      pFieldE->bytes = bufLen;
8,028✔
2177
      pField->bytes = bufLen;
8,028✔
2178
    }
2179
    if (!p) return terrno;
8,028!
2180
    pResultInfo->convertBuf[i] = p;
8,028✔
2181

2182
    for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
6,469,973✔
2183
      int32_t code = decimalToStr((DecimalWord*)(pResultInfo->pCol[i].pData + j * tDataTypes[type].bytes), type,
6,461,945✔
2184
                                  pFieldE->precision, pFieldE->scale, p, bufLen);
6,461,945✔
2185
      p += bufLen;
6,461,945✔
2186
      if (TSDB_CODE_SUCCESS != code) {
6,461,945!
2187
        return code;
×
2188
      }
2189
    }
2190
    pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
8,028✔
2191
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
8,028✔
2192
  }
2193
  return 0;
1,348,723✔
2194
}
2195

2196
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols) {
1,270✔
2197
  return sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) * 3 + sizeof(uint64_t) +
1,270✔
2198
         numOfCols * (sizeof(int8_t) + sizeof(int32_t));
2199
}
2200

2201
static int32_t estimateJsonLen(SReqResultInfo* pResultInfo) {
635✔
2202
  char*   p = (char*)pResultInfo->pData;
635✔
2203
  int32_t blockVersion = *(int32_t*)p;
635✔
2204

2205
  int32_t numOfRows = pResultInfo->numOfRows;
635✔
2206
  int32_t numOfCols = pResultInfo->numOfCols;
635✔
2207

2208
  // | version | total length | total rows | total columns | flag seg| block group id | column schema | each column
2209
  // length |
2210
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
635✔
2211
  if (numOfCols != cols) {
635!
2212
    tscError("estimateJsonLen error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2213
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2214
  }
2215

2216
  int32_t  len = getVersion1BlockMetaSize(p, numOfCols);
635✔
2217
  int32_t* colLength = (int32_t*)(p + len);
635✔
2218
  len += sizeof(int32_t) * numOfCols;
635✔
2219

2220
  char* pStart = p + len;
635✔
2221
  for (int32_t i = 0; i < numOfCols; ++i) {
2,895✔
2222
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
2,260!
2223

2224
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
2,260✔
2225
      int32_t* offset = (int32_t*)pStart;
712✔
2226
      int32_t  lenTmp = numOfRows * sizeof(int32_t);
712✔
2227
      len += lenTmp;
712✔
2228
      pStart += lenTmp;
712✔
2229

2230
      int32_t estimateColLen = 0;
712✔
2231
      for (int32_t j = 0; j < numOfRows; ++j) {
3,613✔
2232
        if (offset[j] == -1) {
2,901✔
2233
          continue;
223✔
2234
        }
2235
        char* data = offset[j] + pStart;
2,678✔
2236

2237
        int32_t jsonInnerType = *data;
2,678✔
2238
        char*   jsonInnerData = data + CHAR_BYTES;
2,678✔
2239
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
2,678✔
2240
          estimateColLen += (VARSTR_HEADER_SIZE + strlen(TSDB_DATA_NULL_STR_L));
72✔
2241
        } else if (tTagIsJson(data)) {
2,606✔
2242
          estimateColLen += (VARSTR_HEADER_SIZE + ((const STag*)(data))->len);
844✔
2243
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
1,762✔
2244
          estimateColLen += varDataTLen(jsonInnerData) + CHAR_BYTES * 2;
1,466✔
2245
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
296✔
2246
          estimateColLen += (VARSTR_HEADER_SIZE + 32);
216✔
2247
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
80!
2248
          estimateColLen += (VARSTR_HEADER_SIZE + 5);
80✔
2249
        } else {
2250
          tscError("estimateJsonLen error: invalid type:%d", jsonInnerType);
×
2251
          return -1;
×
2252
        }
2253
      }
2254
      len += TMAX(colLen, estimateColLen);
712✔
2255
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,548!
2256
      int32_t lenTmp = numOfRows * sizeof(int32_t);
396✔
2257
      len += (lenTmp + colLen);
396✔
2258
      pStart += lenTmp;
396✔
2259
    } else {
2260
      int32_t lenTmp = BitmapLen(pResultInfo->numOfRows);
1,152✔
2261
      len += (lenTmp + colLen);
1,152✔
2262
      pStart += lenTmp;
1,152✔
2263
    }
2264
    pStart += colLen;
2,260✔
2265
  }
2266

2267
  // Ensure the complete structure of the block, including the blankfill field,
2268
  // even though it is not used on the client side.
2269
  len += sizeof(bool);
635✔
2270
  return len;
635✔
2271
}
2272

2273
static int32_t doConvertJson(SReqResultInfo* pResultInfo) {
1,348,978✔
2274
  int32_t numOfRows = pResultInfo->numOfRows;
1,348,978✔
2275
  int32_t numOfCols = pResultInfo->numOfCols;
1,348,978✔
2276
  bool    needConvert = false;
1,348,978✔
2277
  for (int32_t i = 0; i < numOfCols; ++i) {
6,764,515✔
2278
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
5,416,172✔
2279
      needConvert = true;
635✔
2280
      break;
635✔
2281
    }
2282
  }
2283

2284
  if (!needConvert) {
1,348,978✔
2285
    return TSDB_CODE_SUCCESS;
1,348,345✔
2286
  }
2287

2288
  tscDebug("start to convert form json format string");
633✔
2289

2290
  char*   p = (char*)pResultInfo->pData;
633✔
2291
  int32_t blockVersion = *(int32_t*)p;
633✔
2292
  int32_t dataLen = estimateJsonLen(pResultInfo);
633✔
2293
  if (dataLen <= 0) {
635!
2294
    tscError("doConvertJson error: estimateJsonLen failed");
×
2295
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2296
  }
2297

2298
  taosMemoryFreeClear(pResultInfo->convertJson);
635!
2299
  pResultInfo->convertJson = taosMemoryCalloc(1, dataLen);
635!
2300
  if (pResultInfo->convertJson == NULL) return terrno;
635!
2301
  char* p1 = pResultInfo->convertJson;
635✔
2302

2303
  int32_t totalLen = 0;
635✔
2304
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
635✔
2305
  if (numOfCols != cols) {
635!
2306
    tscError("doConvertJson error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2307
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2308
  }
2309

2310
  int32_t len = getVersion1BlockMetaSize(p, numOfCols);
635✔
2311
  (void)memcpy(p1, p, len);
635✔
2312

2313
  p += len;
635✔
2314
  p1 += len;
635✔
2315
  totalLen += len;
635✔
2316

2317
  len = sizeof(int32_t) * numOfCols;
635✔
2318
  int32_t* colLength = (int32_t*)p;
635✔
2319
  int32_t* colLength1 = (int32_t*)p1;
635✔
2320
  (void)memcpy(p1, p, len);
635✔
2321
  p += len;
635✔
2322
  p1 += len;
635✔
2323
  totalLen += len;
635✔
2324

2325
  char* pStart = p;
635✔
2326
  char* pStart1 = p1;
635✔
2327
  for (int32_t i = 0; i < numOfCols; ++i) {
2,895✔
2328
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
2,260!
2329
    int32_t colLen1 = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength1[i]) : colLength1[i];
2,260!
2330
    if (colLen >= dataLen) {
2,260!
2331
      tscError("doConvertJson error: colLen:%d >= dataLen:%d", colLen, dataLen);
×
2332
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2333
    }
2334
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
2,260✔
2335
      int32_t* offset = (int32_t*)pStart;
712✔
2336
      int32_t* offset1 = (int32_t*)pStart1;
712✔
2337
      len = numOfRows * sizeof(int32_t);
712✔
2338
      (void)memcpy(pStart1, pStart, len);
712✔
2339
      pStart += len;
712✔
2340
      pStart1 += len;
712✔
2341
      totalLen += len;
712✔
2342

2343
      len = 0;
712✔
2344
      for (int32_t j = 0; j < numOfRows; ++j) {
3,613✔
2345
        if (offset[j] == -1) {
2,901✔
2346
          continue;
223✔
2347
        }
2348
        char* data = offset[j] + pStart;
2,678✔
2349

2350
        int32_t jsonInnerType = *data;
2,678✔
2351
        char*   jsonInnerData = data + CHAR_BYTES;
2,678✔
2352
        char    dst[TSDB_MAX_JSON_TAG_LEN] = {0};
2,678✔
2353
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
2,678✔
2354
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s", TSDB_DATA_NULL_STR_L);
72✔
2355
          varDataSetLen(dst, strlen(varDataVal(dst)));
72✔
2356
        } else if (tTagIsJson(data)) {
2,606✔
2357
          char* jsonString = NULL;
844✔
2358
          parseTagDatatoJson(data, &jsonString, pResultInfo->charsetCxt);
844✔
2359
          if (jsonString == NULL) {
844!
2360
            tscError("doConvertJson error: parseTagDatatoJson failed");
×
2361
            return terrno;
×
2362
          }
2363
          STR_TO_VARSTR(dst, jsonString);
844✔
2364
          taosMemoryFree(jsonString);
844!
2365
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
1,762✔
2366
          *(char*)varDataVal(dst) = '\"';
1,466✔
2367
          int32_t length = taosUcs4ToMbs((TdUcs4*)varDataVal(jsonInnerData), varDataLen(jsonInnerData),
1,466✔
2368
                                         varDataVal(dst) + CHAR_BYTES, pResultInfo->charsetCxt);
2369
          if (length <= 0) {
1,466✔
2370
            tscError("charset:%s to %s. convert failed.", DEFAULT_UNICODE_ENCODEC,
4!
2371
                     pResultInfo->charsetCxt != NULL ? ((SConvInfo*)(pResultInfo->charsetCxt))->charset : tsCharset);
2372
            length = 0;
4✔
2373
          }
2374
          varDataSetLen(dst, length + CHAR_BYTES * 2);
1,466✔
2375
          *(char*)POINTER_SHIFT(varDataVal(dst), length + CHAR_BYTES) = '\"';
1,466✔
2376
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
296✔
2377
          double jsonVd = *(double*)(jsonInnerData);
216✔
2378
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%.9lf", jsonVd);
216✔
2379
          varDataSetLen(dst, strlen(varDataVal(dst)));
216✔
2380
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
80!
2381
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s",
80✔
2382
                         (*((char*)jsonInnerData) == 1) ? "true" : "false");
80✔
2383
          varDataSetLen(dst, strlen(varDataVal(dst)));
80✔
2384
        } else {
2385
          tscError("doConvertJson error: invalid type:%d", jsonInnerType);
×
2386
          return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2387
        }
2388

2389
        offset1[j] = len;
2,678✔
2390
        (void)memcpy(pStart1 + len, dst, varDataTLen(dst));
2,678✔
2391
        len += varDataTLen(dst);
2,678✔
2392
      }
2393
      colLen1 = len;
712✔
2394
      totalLen += colLen1;
712✔
2395
      colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len;
712!
2396
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,548!
2397
      len = numOfRows * sizeof(int32_t);
396✔
2398
      (void)memcpy(pStart1, pStart, len);
396✔
2399
      pStart += len;
396✔
2400
      pStart1 += len;
396✔
2401
      totalLen += len;
396✔
2402
      totalLen += colLen;
396✔
2403
      (void)memcpy(pStart1, pStart, colLen);
396✔
2404
    } else {
2405
      len = BitmapLen(pResultInfo->numOfRows);
1,152✔
2406
      (void)memcpy(pStart1, pStart, len);
1,152✔
2407
      pStart += len;
1,152✔
2408
      pStart1 += len;
1,152✔
2409
      totalLen += len;
1,152✔
2410
      totalLen += colLen;
1,152✔
2411
      (void)memcpy(pStart1, pStart, colLen);
1,152✔
2412
    }
2413
    pStart += colLen;
2,260✔
2414
    pStart1 += colLen1;
2,260✔
2415
  }
2416

2417
  // Ensure the complete structure of the block, including the blankfill field,
2418
  // even though it is not used on the client side.
2419
  // (void)memcpy(pStart1, pStart, sizeof(bool));
2420
  totalLen += sizeof(bool);
635✔
2421

2422
  *(int32_t*)(pResultInfo->convertJson + 4) = totalLen;
635✔
2423
  pResultInfo->pData = pResultInfo->convertJson;
635✔
2424
  return TSDB_CODE_SUCCESS;
635✔
2425
}
2426

2427
int32_t setResultDataPtr(SReqResultInfo* pResultInfo, bool convertUcs4, bool isStmt) {
1,419,734✔
2428
  bool convertForDecimal = convertUcs4;
1,419,734✔
2429
  if (pResultInfo == NULL || pResultInfo->numOfCols <= 0 || pResultInfo->fields == NULL) {
1,419,734!
2430
    tscError("setResultDataPtr paras error");
×
2431
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2432
  }
2433

2434
  if (pResultInfo->numOfRows == 0) {
1,419,739✔
2435
    return TSDB_CODE_SUCCESS;
70,764✔
2436
  }
2437

2438
  if (pResultInfo->pData == NULL) {
1,348,975!
2439
    tscError("setResultDataPtr error: pData is NULL");
×
2440
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2441
  }
2442

2443
  int32_t code = doPrepareResPtr(pResultInfo);
1,348,975✔
2444
  if (code != TSDB_CODE_SUCCESS) {
1,348,983!
2445
    return code;
×
2446
  }
2447
  code = doConvertJson(pResultInfo);
1,348,983✔
2448
  if (code != TSDB_CODE_SUCCESS) {
1,348,974!
2449
    return code;
×
2450
  }
2451

2452
  char* p = (char*)pResultInfo->pData;
1,348,974✔
2453

2454
  // version:
2455
  int32_t blockVersion = *(int32_t*)p;
1,348,974✔
2456
  p += sizeof(int32_t);
1,348,974✔
2457

2458
  int32_t dataLen = *(int32_t*)p;
1,348,974✔
2459
  p += sizeof(int32_t);
1,348,974✔
2460

2461
  int32_t rows = *(int32_t*)p;
1,348,974✔
2462
  p += sizeof(int32_t);
1,348,974✔
2463

2464
  int32_t cols = *(int32_t*)p;
1,348,974✔
2465
  p += sizeof(int32_t);
1,348,974✔
2466

2467
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
1,348,974!
2468
    tscError("setResultDataPtr paras error:rows;%d numOfRows:%" PRId64 " cols:%d numOfCols:%d", rows,
×
2469
             pResultInfo->numOfRows, cols, pResultInfo->numOfCols);
2470
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2471
  }
2472

2473
  int32_t hasColumnSeg = *(int32_t*)p;
1,348,977✔
2474
  p += sizeof(int32_t);
1,348,977✔
2475

2476
  uint64_t groupId = taosGetUInt64Aligned((uint64_t*)p);
1,348,977✔
2477
  p += sizeof(uint64_t);
1,348,977✔
2478

2479
  // check fields
2480
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,765,028✔
2481
    int8_t type = *(int8_t*)p;
5,416,075✔
2482
    p += sizeof(int8_t);
5,416,075✔
2483

2484
    int32_t bytes = *(int32_t*)p;
5,416,075✔
2485
    p += sizeof(int32_t);
5,416,075✔
2486

2487
    if (IS_DECIMAL_TYPE(type) && pResultInfo->fields[i].precision == 0) {
5,416,075!
2488
      extractDecimalTypeInfoFromBytes(&bytes, &pResultInfo->fields[i].precision, &pResultInfo->fields[i].scale);
×
2489
    }
2490
  }
2491

2492
  int32_t* colLength = (int32_t*)p;
1,348,953✔
2493
  p += sizeof(int32_t) * pResultInfo->numOfCols;
1,348,953✔
2494

2495
  char* pStart = p;
1,348,953✔
2496
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,764,689✔
2497
    if ((pStart - pResultInfo->pData) >= dataLen) {
5,415,766!
2498
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2499
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2500
    }
2501
    if (blockVersion == BLOCK_VERSION_1) {
5,415,766✔
2502
      colLength[i] = htonl(colLength[i]);
3,503,477✔
2503
    }
2504
    if (colLength[i] >= dataLen) {
5,415,766!
2505
      tscError("invalid colLength %d, dataLen %d", colLength[i], dataLen);
×
2506
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2507
    }
2508
    if (IS_INVALID_TYPE(pResultInfo->fields[i].type)) {
5,415,766✔
2509
      tscError("invalid type %d", pResultInfo->fields[i].type);
8!
2510
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2511
    }
2512
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
5,415,758✔
2513
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
1,365,966✔
2514
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
1,365,966✔
2515
    } else {
2516
      pResultInfo->pCol[i].nullbitmap = pStart;
4,049,792✔
2517
      pStart += BitmapLen(pResultInfo->numOfRows);
4,049,792✔
2518
    }
2519

2520
    pResultInfo->pCol[i].pData = pStart;
5,415,758✔
2521
    pResultInfo->length[i] =
10,831,494✔
2522
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
5,415,758✔
2523
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
5,415,736✔
2524

2525
    pStart += colLength[i];
5,415,736✔
2526
  }
2527

2528
  p = pStart;
1,348,923✔
2529
  // bool blankFill = *(bool*)p;
2530
  p += sizeof(bool);
1,348,923✔
2531
  int32_t offset = p - pResultInfo->pData;
1,348,923✔
2532
  if (offset > dataLen) {
1,348,923!
2533
    tscError("invalid offset %d, dataLen %d", offset, dataLen);
×
2534
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2535
  }
2536

2537
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
2538
  if (convertUcs4) {
1,348,923✔
2539
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
1,348,669✔
2540
  }
2541
#endif
2542
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
1,348,980!
2543
    code = convertDecimalType(pResultInfo);
1,348,725✔
2544
  }
2545
  return code;
1,348,977✔
2546
}
2547

2548
char* getDbOfConnection(STscObj* pObj) {
3,274,258✔
2549
  terrno = TSDB_CODE_SUCCESS;
3,274,258✔
2550
  char* p = NULL;
3,274,341✔
2551
  (void)taosThreadMutexLock(&pObj->mutex);
3,274,341✔
2552
  size_t len = strlen(pObj->db);
3,274,517✔
2553
  if (len > 0) {
3,274,517✔
2554
    p = taosStrndup(pObj->db, tListLen(pObj->db));
2,733,868!
2555
    if (p == NULL) {
2,733,768!
2556
      tscError("failed to taosStrndup db name");
×
2557
    }
2558
  }
2559

2560
  (void)taosThreadMutexUnlock(&pObj->mutex);
3,274,417✔
2561
  return p;
3,274,452✔
2562
}
2563

2564
void setConnectionDB(STscObj* pTscObj, const char* db) {
7,835✔
2565
  if (db == NULL || pTscObj == NULL) {
7,835!
2566
    tscError("setConnectionDB para is NULL");
×
2567
    return;
×
2568
  }
2569

2570
  (void)taosThreadMutexLock(&pTscObj->mutex);
7,836✔
2571
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
7,836✔
2572
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
7,836✔
2573
}
2574

2575
void resetConnectDB(STscObj* pTscObj) {
×
2576
  if (pTscObj == NULL) {
×
2577
    return;
×
2578
  }
2579

2580
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
2581
  pTscObj->db[0] = 0;
×
2582
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
2583
}
2584

2585
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4,
1,065,571✔
2586
                              bool isStmt) {
2587
  if (pResultInfo == NULL || pRsp == NULL) {
1,065,571!
2588
    tscError("setQueryResultFromRsp paras is null");
×
2589
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2590
  }
2591

2592
  taosMemoryFreeClear(pResultInfo->pRspMsg);
1,065,576!
2593
  pResultInfo->pRspMsg = (const char*)pRsp;
1,065,576✔
2594
  pResultInfo->numOfRows = htobe64(pRsp->numOfRows);
1,065,576✔
2595
  pResultInfo->current = 0;
1,065,572✔
2596
  pResultInfo->completed = (pRsp->completed == 1);
1,065,572✔
2597
  pResultInfo->precision = pRsp->precision;
1,065,572✔
2598

2599
  // decompress data if needed
2600
  int32_t payloadLen = htonl(pRsp->payloadLen);
1,065,572✔
2601

2602
  if (pRsp->compressed) {
1,065,572✔
2603
    if (pResultInfo->decompBuf == NULL) {
654✔
2604
      pResultInfo->decompBuf = taosMemoryMalloc(payloadLen);
15!
2605
      if (pResultInfo->decompBuf == NULL) {
15!
2606
        tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2607
        return terrno;
×
2608
      }
2609
      pResultInfo->decompBufSize = payloadLen;
15✔
2610
    } else {
2611
      if (pResultInfo->decompBufSize < payloadLen) {
639✔
2612
        char* p = taosMemoryRealloc(pResultInfo->decompBuf, payloadLen);
28!
2613
        if (p == NULL) {
28!
2614
          tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2615
          return terrno;
×
2616
        }
2617

2618
        pResultInfo->decompBuf = p;
28✔
2619
        pResultInfo->decompBufSize = payloadLen;
28✔
2620
      }
2621
    }
2622
  }
2623

2624
  if (payloadLen > 0) {
1,065,572✔
2625
    int32_t compLen = *(int32_t*)pRsp->data;
994,810✔
2626
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
994,810✔
2627

2628
    char* pStart = (char*)pRsp->data + sizeof(int32_t) * 2;
994,810✔
2629

2630
    if (pRsp->compressed && compLen < rawLen) {
994,810!
2631
      int32_t len = tsDecompressString(pStart, compLen, 1, pResultInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
654✔
2632
      if (len < 0) {
654!
2633
        tscError("tsDecompressString failed");
×
2634
        return terrno ? terrno : TSDB_CODE_FAILED;
×
2635
      }
2636
      if (len != rawLen) {
654!
2637
        tscError("tsDecompressString failed, len:%d != rawLen:%d", len, rawLen);
×
2638
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2639
      }
2640
      pResultInfo->pData = pResultInfo->decompBuf;
654✔
2641
      pResultInfo->payloadLen = rawLen;
654✔
2642
    } else {
2643
      pResultInfo->pData = pStart;
994,156✔
2644
      pResultInfo->payloadLen = htonl(pRsp->compLen);
994,156✔
2645
      if (pRsp->compLen != pRsp->payloadLen) {
994,156!
2646
        tscError("pRsp->compLen:%d != pRsp->payloadLen:%d", pRsp->compLen, pRsp->payloadLen);
×
2647
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2648
      }
2649
    }
2650
  }
2651

2652
  // TODO handle the compressed case
2653
  pResultInfo->totalRows += pResultInfo->numOfRows;
1,065,572✔
2654

2655
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
1,065,572✔
2656
  return code;
1,065,580✔
2657
}
2658

2659
TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* details, int maxlen) {
5✔
2660
  TSDB_SERVER_STATUS code = TSDB_SRV_STATUS_UNAVAILABLE;
5✔
2661
  void*              clientRpc = NULL;
5✔
2662
  SServerStatusRsp   statusRsp = {0};
5✔
2663
  SEpSet             epSet = {.inUse = 0, .numOfEps = 1};
5✔
2664
  SRpcMsg  rpcMsg = {.info.ahandle = (void*)0x9527, .info.notFreeAhandle = 1, .msgType = TDMT_DND_SERVER_STATUS};
5✔
2665
  SRpcMsg  rpcRsp = {0};
5✔
2666
  SRpcInit rpcInit = {0};
5✔
2667
  char     pass[TSDB_PASSWORD_LEN + 1] = {0};
5✔
2668

2669
  rpcInit.label = "CHK";
5✔
2670
  rpcInit.numOfThreads = 1;
5✔
2671
  rpcInit.cfp = NULL;
5✔
2672
  rpcInit.sessions = 16;
5✔
2673
  rpcInit.connType = TAOS_CONN_CLIENT;
5✔
2674
  rpcInit.idleTime = tsShellActivityTimer * 1000;
5✔
2675
  rpcInit.compressSize = tsCompressMsgSize;
5✔
2676
  rpcInit.user = "_dnd";
5✔
2677

2678
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
5✔
2679
  connLimitNum = TMAX(connLimitNum, 10);
5✔
2680
  connLimitNum = TMIN(connLimitNum, 500);
5✔
2681
  rpcInit.connLimitNum = connLimitNum;
5✔
2682
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
5✔
2683
  rpcInit.readTimeout = tsReadTimeout;
5✔
2684
  rpcInit.ipv6 = tsEnableIpv6;
5✔
2685
  if (TSDB_CODE_SUCCESS != taosVersionStrToInt(td_version, &rpcInit.compatibilityVer)) {
5!
2686
    tscError("faild to convert taos version from str to int, errcode:%s", terrstr());
×
2687
    goto _OVER;
×
2688
  }
2689

2690
  clientRpc = rpcOpen(&rpcInit);
5✔
2691
  if (clientRpc == NULL) {
5!
2692
    code = terrno;
×
2693
    tscError("failed to init server status client since %s", tstrerror(code));
×
2694
    goto _OVER;
×
2695
  }
2696

2697
  if (fqdn == NULL) {
5!
2698
    fqdn = tsLocalFqdn;
5✔
2699
  }
2700

2701
  if (port == 0) {
5!
2702
    port = tsServerPort;
5✔
2703
  }
2704

2705
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
5✔
2706
  epSet.eps[0].port = (uint16_t)port;
5✔
2707
  int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
5✔
2708
  if (TSDB_CODE_SUCCESS != ret) {
5!
2709
    tscError("failed to send recv since %s", tstrerror(ret));
×
2710
    goto _OVER;
×
2711
  }
2712

2713
  if (rpcRsp.code != 0 || rpcRsp.contLen <= 0 || rpcRsp.pCont == NULL) {
5!
2714
    tscError("failed to send server status req since %s", terrstr());
1!
2715
    goto _OVER;
1✔
2716
  }
2717

2718
  if (tDeserializeSServerStatusRsp(rpcRsp.pCont, rpcRsp.contLen, &statusRsp) != 0) {
4!
2719
    tscError("failed to parse server status rsp since %s", terrstr());
×
2720
    goto _OVER;
×
2721
  }
2722

2723
  code = statusRsp.statusCode;
4✔
2724
  if (details != NULL) {
4!
2725
    tstrncpy(details, statusRsp.details, maxlen);
4✔
2726
  }
2727

2728
_OVER:
×
2729
  if (clientRpc != NULL) {
5!
2730
    rpcClose(clientRpc);
5✔
2731
  }
2732
  if (rpcRsp.pCont != NULL) {
5✔
2733
    rpcFreeCont(rpcRsp.pCont);
4✔
2734
  }
2735
  return code;
5✔
2736
}
2737

2738
int32_t appendTbToReq(SHashObj* pHash, int32_t pos1, int32_t len1, int32_t pos2, int32_t len2, const char* str,
4✔
2739
                      int32_t acctId, char* db) {
2740
  SName name = {0};
4✔
2741

2742
  if (len1 <= 0) {
4!
2743
    return -1;
×
2744
  }
2745

2746
  const char* dbName = db;
4✔
2747
  const char* tbName = NULL;
4✔
2748
  int32_t     dbLen = 0;
4✔
2749
  int32_t     tbLen = 0;
4✔
2750
  if (len2 > 0) {
4!
2751
    dbName = str + pos1;
×
2752
    dbLen = len1;
×
2753
    tbName = str + pos2;
×
2754
    tbLen = len2;
×
2755
  } else {
2756
    dbLen = strlen(db);
4✔
2757
    tbName = str + pos1;
4✔
2758
    tbLen = len1;
4✔
2759
  }
2760

2761
  if (dbLen <= 0 || tbLen <= 0) {
4!
2762
    return -1;
×
2763
  }
2764

2765
  if (tNameSetDbName(&name, acctId, dbName, dbLen)) {
4!
2766
    return -1;
×
2767
  }
2768

2769
  if (tNameAddTbName(&name, tbName, tbLen)) {
4!
2770
    return -1;
×
2771
  }
2772

2773
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
4✔
2774
  (void)snprintf(dbFName, TSDB_DB_FNAME_LEN, "%d.%.*s", acctId, dbLen, dbName);
4✔
2775

2776
  STablesReq* pDb = taosHashGet(pHash, dbFName, strlen(dbFName));
4✔
2777
  if (pDb) {
4!
2778
    if (NULL == taosArrayPush(pDb->pTables, &name)) {
×
2779
      return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2780
    }
2781
  } else {
2782
    STablesReq db;
2783
    db.pTables = taosArrayInit(20, sizeof(SName));
4✔
2784
    if (NULL == db.pTables) {
4!
2785
      return terrno;
×
2786
    }
2787
    tstrncpy(db.dbFName, dbFName, TSDB_DB_FNAME_LEN);
4✔
2788
    if (NULL == taosArrayPush(db.pTables, &name)) {
8!
2789
      return terrno;
×
2790
    }
2791
    TSC_ERR_RET(taosHashPut(pHash, dbFName, strlen(dbFName), &db, sizeof(db)));
4!
2792
  }
2793

2794
  return TSDB_CODE_SUCCESS;
4✔
2795
}
2796

2797
int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq) {
4✔
2798
  SHashObj* pHash = taosHashInit(3, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
4✔
2799
  if (NULL == pHash) {
4!
2800
    return terrno;
×
2801
  }
2802

2803
  bool    inEscape = false;
4✔
2804
  int32_t code = 0;
4✔
2805
  void*   pIter = NULL;
4✔
2806

2807
  int32_t vIdx = 0;
4✔
2808
  int32_t vPos[2];
2809
  int32_t vLen[2];
2810

2811
  (void)memset(vPos, -1, sizeof(vPos));
4✔
2812
  (void)memset(vLen, 0, sizeof(vLen));
4✔
2813

2814
  for (int32_t i = 0;; ++i) {
18✔
2815
    if (0 == *(tbList + i)) {
18✔
2816
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
4!
2817
        vLen[vIdx] = i - vPos[vIdx];
4✔
2818
      }
2819

2820
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
4✔
2821
      if (code) {
4!
2822
        goto _return;
×
2823
      }
2824

2825
      break;
4✔
2826
    }
2827

2828
    if ('`' == *(tbList + i)) {
14!
2829
      inEscape = !inEscape;
×
2830
      if (!inEscape) {
×
2831
        if (vPos[vIdx] >= 0) {
×
2832
          vLen[vIdx] = i - vPos[vIdx];
×
2833
        } else {
2834
          goto _return;
×
2835
        }
2836
      }
2837

2838
      continue;
×
2839
    }
2840

2841
    if (inEscape) {
14!
2842
      if (vPos[vIdx] < 0) {
×
2843
        vPos[vIdx] = i;
×
2844
      }
2845
      continue;
×
2846
    }
2847

2848
    if ('.' == *(tbList + i)) {
14!
2849
      if (vPos[vIdx] < 0) {
×
2850
        goto _return;
×
2851
      }
2852
      if (vLen[vIdx] <= 0) {
×
2853
        vLen[vIdx] = i - vPos[vIdx];
×
2854
      }
2855
      vIdx++;
×
2856
      if (vIdx >= 2) {
×
2857
        goto _return;
×
2858
      }
2859
      continue;
×
2860
    }
2861

2862
    if (',' == *(tbList + i)) {
14!
2863
      if (vPos[vIdx] < 0) {
×
2864
        goto _return;
×
2865
      }
2866
      if (vLen[vIdx] <= 0) {
×
2867
        vLen[vIdx] = i - vPos[vIdx];
×
2868
      }
2869

2870
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
×
2871
      if (code) {
×
2872
        goto _return;
×
2873
      }
2874

2875
      (void)memset(vPos, -1, sizeof(vPos));
×
2876
      (void)memset(vLen, 0, sizeof(vLen));
×
2877
      vIdx = 0;
×
2878
      continue;
×
2879
    }
2880

2881
    if (' ' == *(tbList + i) || '\r' == *(tbList + i) || '\t' == *(tbList + i) || '\n' == *(tbList + i)) {
14!
2882
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
×
2883
        vLen[vIdx] = i - vPos[vIdx];
×
2884
      }
2885
      continue;
×
2886
    }
2887

2888
    if (('a' <= *(tbList + i) && 'z' >= *(tbList + i)) || ('A' <= *(tbList + i) && 'Z' >= *(tbList + i)) ||
14!
2889
        ('0' <= *(tbList + i) && '9' >= *(tbList + i)) || ('_' == *(tbList + i))) {
1!
2890
      if (vLen[vIdx] > 0) {
14!
2891
        goto _return;
×
2892
      }
2893
      if (vPos[vIdx] < 0) {
14✔
2894
        vPos[vIdx] = i;
4✔
2895
      }
2896
      continue;
14✔
2897
    }
2898

2899
    goto _return;
×
2900
  }
2901

2902
  int32_t dbNum = taosHashGetSize(pHash);
4✔
2903
  *pReq = taosArrayInit(dbNum, sizeof(STablesReq));
4✔
2904
  if (NULL == pReq) {
4!
2905
    TSC_ERR_JRET(terrno);
×
2906
  }
2907
  pIter = taosHashIterate(pHash, NULL);
4✔
2908
  while (pIter) {
8✔
2909
    STablesReq* pDb = (STablesReq*)pIter;
4✔
2910
    if (NULL == taosArrayPush(*pReq, pDb)) {
8!
2911
      TSC_ERR_JRET(terrno);
×
2912
    }
2913
    pIter = taosHashIterate(pHash, pIter);
4✔
2914
  }
2915

2916
  taosHashCleanup(pHash);
4✔
2917

2918
  return TSDB_CODE_SUCCESS;
4✔
2919

2920
_return:
×
2921

2922
  terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
2923

2924
  pIter = taosHashIterate(pHash, NULL);
×
2925
  while (pIter) {
×
2926
    STablesReq* pDb = (STablesReq*)pIter;
×
2927
    taosArrayDestroy(pDb->pTables);
×
2928
    pIter = taosHashIterate(pHash, pIter);
×
2929
  }
2930

2931
  taosHashCleanup(pHash);
×
2932

2933
  return terrno;
×
2934
}
2935

2936
void syncCatalogFn(SMetaData* pResult, void* param, int32_t code) {
4✔
2937
  SSyncQueryParam* pParam = param;
4✔
2938
  pParam->pRequest->code = code;
4✔
2939

2940
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
4!
2941
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
2942
  }
2943
}
4✔
2944

2945
void syncQueryFn(void* param, void* res, int32_t code) {
3,166,339✔
2946
  SSyncQueryParam* pParam = param;
3,166,339✔
2947
  pParam->pRequest = res;
3,166,339✔
2948

2949
  if (pParam->pRequest) {
3,166,339!
2950
    pParam->pRequest->code = code;
3,166,361✔
2951
    clientOperateReport(pParam->pRequest);
3,166,361✔
2952
  }
2953

2954
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
3,166,356!
2955
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
2956
  }
2957
}
3,166,390✔
2958

2959
void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
3,165,520✔
2960
                        int8_t source) {
2961
  if (sql == NULL || NULL == fp) {
3,165,520!
2962
    terrno = TSDB_CODE_INVALID_PARA;
×
2963
    if (fp) {
×
2964
      fp(param, NULL, terrno);
×
2965
    }
2966

2967
    return;
1✔
2968
  }
2969

2970
  size_t sqlLen = strlen(sql);
3,165,648✔
2971
  if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) {
3,165,648✔
2972
    tscError("conn:0x%" PRIx64 ", sql string exceeds max length:%d", connId, TSDB_MAX_ALLOWED_SQL_LEN);
1!
2973
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
1✔
2974
    fp(param, NULL, terrno);
1✔
2975
    return;
1✔
2976
  }
2977

2978
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, sql:%s", connId, sql);
3,165,647✔
2979

2980
  SRequestObj* pRequest = NULL;
3,165,647✔
2981
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, 0);
3,165,647✔
2982
  if (code != TSDB_CODE_SUCCESS) {
3,165,608!
2983
    terrno = code;
×
2984
    fp(param, NULL, terrno);
×
2985
    return;
×
2986
  }
2987

2988
  pRequest->source = source;
3,165,608✔
2989
  pRequest->body.queryFp = fp;
3,165,608✔
2990
  doAsyncQuery(pRequest, false);
3,165,608✔
2991
}
2992

2993
void taosAsyncQueryImplWithReqid(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
×
2994
                                 int64_t reqid) {
2995
  if (sql == NULL || NULL == fp) {
×
2996
    terrno = TSDB_CODE_INVALID_PARA;
×
2997
    if (fp) {
×
2998
      fp(param, NULL, terrno);
×
2999
    }
3000

3001
    return;
×
3002
  }
3003

3004
  size_t sqlLen = strlen(sql);
×
3005
  if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) {
×
3006
    tscError("conn:0x%" PRIx64 ", QID:0x%" PRIx64 ", sql string exceeds max length:%d", connId, reqid,
×
3007
             TSDB_MAX_ALLOWED_SQL_LEN);
3008
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
×
3009
    fp(param, NULL, terrno);
×
3010
    return;
×
3011
  }
3012

3013
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, QID:0x%" PRIx64 ", sql:%s", connId, reqid, sql);
×
3014

3015
  SRequestObj* pRequest = NULL;
×
3016
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, reqid);
×
3017
  if (code != TSDB_CODE_SUCCESS) {
×
3018
    terrno = code;
×
3019
    fp(param, NULL, terrno);
×
3020
    return;
×
3021
  }
3022

3023
  pRequest->body.queryFp = fp;
×
3024
  doAsyncQuery(pRequest, false);
×
3025
}
3026

3027
TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly, int8_t source) {
3,164,999✔
3028
  if (NULL == taos) {
3,164,999!
3029
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3030
    return NULL;
×
3031
  }
3032

3033
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
3,164,999!
3034
  if (NULL == param) {
3,165,165!
3035
    return NULL;
×
3036
  }
3037
  int32_t code = tsem_init(&param->sem, 0, 0);
3,165,165✔
3038
  if (TSDB_CODE_SUCCESS != code) {
3,165,074!
3039
    taosMemoryFree(param);
×
3040
    return NULL;
×
3041
  }
3042

3043
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
3,165,074✔
3044
  code = tsem_wait(&param->sem);
3,164,872✔
3045
  if (TSDB_CODE_SUCCESS != code) {
3,165,101!
3046
    taosMemoryFree(param);
×
3047
    return NULL;
×
3048
  }
3049
  code = tsem_destroy(&param->sem);
3,165,101✔
3050
  if (TSDB_CODE_SUCCESS != code) {
3,165,097!
3051
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3052
  }
3053

3054
  SRequestObj* pRequest = NULL;
3,165,141✔
3055
  if (param->pRequest != NULL) {
3,165,141✔
3056
    param->pRequest->syncQuery = true;
3,165,140✔
3057
    pRequest = param->pRequest;
3,165,140✔
3058
    param->pRequest->inCallback = false;
3,165,140✔
3059
  }
3060
  taosMemoryFree(param);
3,165,141!
3061

3062
  // tscDebug("QID:0x%" PRIx64 ", taos_query end, conn:0x%" PRIx64 ", res:%p", pRequest ? pRequest->requestId : 0,
3063
  //          *(int64_t*)taos, pRequest);
3064

3065
  return pRequest;
3,165,184✔
3066
}
3067

3068
TAOS_RES* taosQueryImplWithReqid(TAOS* taos, const char* sql, bool validateOnly, int64_t reqid) {
×
3069
  if (NULL == taos) {
×
3070
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3071
    return NULL;
×
3072
  }
3073

3074
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
×
3075
  if (param == NULL) {
×
3076
    return NULL;
×
3077
  }
3078
  int32_t code = tsem_init(&param->sem, 0, 0);
×
3079
  if (TSDB_CODE_SUCCESS != code) {
×
3080
    taosMemoryFree(param);
×
3081
    return NULL;
×
3082
  }
3083

3084
  taosAsyncQueryImplWithReqid(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, reqid);
×
3085
  code = tsem_wait(&param->sem);
×
3086
  if (TSDB_CODE_SUCCESS != code) {
×
3087
    taosMemoryFree(param);
×
3088
    return NULL;
×
3089
  }
3090
  SRequestObj* pRequest = NULL;
×
3091
  if (param->pRequest != NULL) {
×
3092
    param->pRequest->syncQuery = true;
×
3093
    pRequest = param->pRequest;
×
3094
  }
3095
  taosMemoryFree(param);
×
3096

3097
  // tscDebug("QID:0x%" PRIx64 ", taos_query end, conn:0x%" PRIx64 ", res:%p", pRequest ? pRequest->requestId : 0,
3098
  //   *(int64_t*)taos, pRequest);
3099

3100
  return pRequest;
×
3101
}
3102

3103
static void fetchCallback(void* pResult, void* param, int32_t code) {
949,913✔
3104
  SRequestObj* pRequest = (SRequestObj*)param;
949,913✔
3105

3106
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
949,913✔
3107

3108
  tscDebug("req:0x%" PRIx64 ", enter scheduler fetch cb, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
949,913✔
3109
           tstrerror(code), pRequest->requestId);
3110

3111
  pResultInfo->pData = pResult;
949,912✔
3112
  pResultInfo->numOfRows = 0;
949,912✔
3113

3114
  if (code != TSDB_CODE_SUCCESS) {
949,912!
3115
    pRequest->code = code;
×
3116
    taosMemoryFreeClear(pResultInfo->pData);
×
3117
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
×
3118
    return;
×
3119
  }
3120

3121
  if (pRequest->code != TSDB_CODE_SUCCESS) {
949,912!
3122
    taosMemoryFreeClear(pResultInfo->pData);
×
3123
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pRequest->code);
×
3124
    return;
×
3125
  }
3126

3127
  pRequest->code = setQueryResultFromRsp(pResultInfo, (const SRetrieveTableRsp*)pResultInfo->pData,
1,899,829✔
3128
                                         pResultInfo->convertUcs4, pRequest->isStmtBind);
949,912✔
3129
  if (pRequest->code != TSDB_CODE_SUCCESS) {
949,917✔
3130
    pResultInfo->numOfRows = 0;
1✔
3131
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self,
1!
3132
             tstrerror(pRequest->code), pRequest->requestId);
3133
  } else {
3134
    tscDebug(
949,916✔
3135
        "req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
3136
        pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed, pRequest->requestId);
3137

3138
    STscObj*            pTscObj = pRequest->pTscObj;
949,917✔
3139
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
949,917✔
3140
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
949,917✔
3141
  }
3142

3143
  pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows);
949,921✔
3144
}
3145

3146
void taosAsyncFetchImpl(SRequestObj* pRequest, __taos_async_fn_t fp, void* param) {
1,035,639✔
3147
  pRequest->body.fetchFp = fp;
1,035,639✔
3148
  ((SSyncQueryParam*)pRequest->body.interParam)->userParam = param;
1,035,639✔
3149

3150
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
1,035,639✔
3151

3152
  // this query has no results or error exists, return directly
3153
  if (taos_num_fields(pRequest) == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
1,035,639!
3154
    pResultInfo->numOfRows = 0;
1✔
3155
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
1✔
3156
    return;
85,720✔
3157
  }
3158

3159
  // all data has returned to App already, no need to try again
3160
  if (pResultInfo->completed) {
1,035,640✔
3161
    // it is a local executed query, no need to do async fetch
3162
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
85,719✔
3163
      if (pResultInfo->localResultFetched) {
2,732✔
3164
        pResultInfo->numOfRows = 0;
1,366✔
3165
        pResultInfo->current = 0;
1,366✔
3166
      } else {
3167
        pResultInfo->localResultFetched = true;
1,366✔
3168
      }
3169
    } else {
3170
      pResultInfo->numOfRows = 0;
82,987✔
3171
    }
3172

3173
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
85,719✔
3174
    return;
85,719✔
3175
  }
3176

3177
  SSchedulerReq req = {
949,921✔
3178
      .syncReq = false,
3179
      .fetchFp = fetchCallback,
3180
      .cbParam = pRequest,
3181
  };
3182

3183
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
949,921✔
3184
  if (TSDB_CODE_SUCCESS != code) {
949,920!
3185
    tscError("0x%" PRIx64 " failed to schedule fetch rows", pRequest->requestId);
×
3186
    // pRequest->body.fetchFp(param, pRequest, code);
3187
  }
3188
}
3189

3190
void doRequestCallback(SRequestObj* pRequest, int32_t code) {
3,165,757✔
3191
  pRequest->inCallback = true;
3,165,757✔
3192
  int64_t this = pRequest->self;
3,165,757✔
3193
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
3,165,757!
3194
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
316!
3195
    code = TSDB_CODE_SUCCESS;
×
3196
    pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
3197
  }
3198

3199
  tscDebug("QID:0x%" PRIx64 ", taos_query end, req:0x%" PRIx64 ", res:%p", pRequest->requestId, pRequest->self,
3,165,757✔
3200
           pRequest);
3201

3202
  if (pRequest->body.queryFp != NULL) {
3,165,758!
3203
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
3,165,800✔
3204
  }
3205

3206
  SRequestObj* pReq = acquireRequest(this);
3,165,779✔
3207
  if (pReq != NULL) {
3,165,844✔
3208
    pReq->inCallback = false;
3,165,790✔
3209
    (void)releaseRequest(this);
3,165,790✔
3210
  }
3211
}
3,165,838✔
3212

3213
int32_t clientParseSql(void* param, const char* dbName, const char* sql, bool parseOnly, const char* effectiveUser,
1,201✔
3214
                       SParseSqlRes* pRes) {
3215
#ifndef TD_ENTERPRISE
3216
  return TSDB_CODE_SUCCESS;
3217
#else
3218
  return clientParseSqlImpl(param, dbName, sql, parseOnly, effectiveUser, pRes);
1,201✔
3219
#endif
3220
}
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