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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

73.88
/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 "clientSession.h"
21
#include "command.h"
22
#include "decimal.h"
23
#include "scheduler.h"
24
#include "tdatablock.h"
25
#include "tdataformat.h"
26
#include "tdef.h"
27
#include "tglobal.h"
28
#include "tmisce.h"
29
#include "tmsg.h"
30
#include "tmsgtype.h"
31
#include "tpagedbuf.h"
32
#include "tref.h"
33
#include "tsched.h"
34
#include "tversion.h"
35

36
static int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet);
37
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo, int32_t totpCode);
38

39
void setQueryRequest(int64_t rId) {
689,585,976✔
40
  SRequestObj* pReq = acquireRequest(rId);
689,585,976✔
41
  if (pReq != NULL) {
689,622,417✔
42
    pReq->isQuery = true;
689,594,061✔
43
    (void)releaseRequest(rId);
689,593,288✔
44
  }
45
}
689,606,813✔
46

47
static bool stringLengthCheck(const char* str, size_t maxsize) {
175,976,377✔
48
  if (str == NULL) {
175,976,377✔
49
    return false;
×
50
  }
51

52
  size_t len = strlen(str);
175,976,377✔
53
  if (len <= 0 || len > maxsize) {
175,976,377✔
54
    return false;
372✔
55
  }
56

57
  return true;
175,980,205✔
58
}
59

60
static bool validateUserName(const char* user) { return stringLengthCheck(user, TSDB_USER_LEN - 1); }
87,747,818✔
61

62
static bool validatePassword(const char* passwd) { return stringLengthCheck(passwd, TSDB_PASSWORD_MAX_LEN); }
87,748,674✔
63

64
static bool validateDbName(const char* db) { return stringLengthCheck(db, TSDB_DB_NAME_LEN - 1); }
480,088✔
65

66
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
87,751,035✔
67
  char key[512] = {0};
87,751,035✔
68
  if (user == NULL) {
87,751,035✔
69
    (void)snprintf(key, sizeof(key), "%s:%s:%d", auth, ip, port);
2,718✔
70
  } else {
71
    (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
87,748,317✔
72
  }
73
  return taosStrdup(key);
87,751,035✔
74
}
75

76
static int32_t escapeToPrinted(char* dst, size_t maxDstLength, const char* src, size_t srcLength) {
633,027✔
77
  if (dst == NULL || src == NULL || srcLength == 0) {
633,027✔
78
    return 0;
531✔
79
  }
80

81
  size_t escapeLength = 0;
632,496✔
82
  for (size_t i = 0; i < srcLength; ++i) {
17,952,594✔
83
    if (src[i] == '\"' || src[i] == '\\' || src[i] == '\b' || src[i] == '\f' || src[i] == '\n' || src[i] == '\r' ||
17,320,098✔
84
        src[i] == '\t') {
17,320,098✔
85
      escapeLength += 1;
×
86
    }
87
  }
88

89
  size_t dstLength = srcLength;
632,496✔
90
  if (escapeLength == 0) {
632,496✔
91
    (void)memcpy(dst, src, srcLength);
632,496✔
92
  } else {
93
    dstLength = 0;
×
94
    for (size_t i = 0; i < srcLength && dstLength <= maxDstLength; i++) {
×
95
      switch (src[i]) {
×
96
        case '\"':
×
97
          dst[dstLength++] = '\\';
×
98
          dst[dstLength++] = '\"';
×
99
          break;
×
100
        case '\\':
×
101
          dst[dstLength++] = '\\';
×
102
          dst[dstLength++] = '\\';
×
103
          break;
×
104
        case '\b':
×
105
          dst[dstLength++] = '\\';
×
106
          dst[dstLength++] = 'b';
×
107
          break;
×
108
        case '\f':
×
109
          dst[dstLength++] = '\\';
×
110
          dst[dstLength++] = 'f';
×
111
          break;
×
112
        case '\n':
×
113
          dst[dstLength++] = '\\';
×
114
          dst[dstLength++] = 'n';
×
115
          break;
×
116
        case '\r':
×
117
          dst[dstLength++] = '\\';
×
118
          dst[dstLength++] = 'r';
×
119
          break;
×
120
        case '\t':
×
121
          dst[dstLength++] = '\\';
×
122
          dst[dstLength++] = 't';
×
123
          break;
×
124
        default:
×
125
          dst[dstLength++] = src[i];
×
126
      }
127
    }
128
  }
129

130
  return dstLength;
632,496✔
131
}
132

133
bool chkRequestKilled(void* param) {
2,147,483,647✔
134
  bool         killed = false;
2,147,483,647✔
135
  SRequestObj* pRequest = acquireRequest((int64_t)param);
2,147,483,647✔
136
  if (NULL == pRequest || pRequest->killed) {
2,147,483,647✔
137
    killed = true;
2,027✔
138
  }
139

140
  (void)releaseRequest((int64_t)param);
2,147,483,647✔
141

142
  return killed;
2,147,483,647✔
143
}
144

145
void cleanupAppInfo() {
1,606,993✔
146
  taosHashCleanup(appInfo.pInstMap);
1,606,993✔
147
  taosHashCleanup(appInfo.pInstMapByClusterId);
1,606,993✔
148
  tscInfo("cluster instance map cleaned");
1,606,993✔
149
}
1,606,993✔
150

151
static int32_t taosConnectImpl(const char* user, const char* auth, int32_t totpCode, const char* db,
152
                               __taos_async_fn_t fp, void* param, SAppInstInfo* pAppInfo, int connType,
153
                               STscObj** pTscObj);
154

155
int32_t taos_connect_by_auth(const char* ip, const char* user, const char* auth, const char* totp, const char* db,
87,751,765✔
156
                             uint16_t port, int connType, STscObj** pObj) {
157
  TSC_ERR_RET(taos_init());
87,751,765✔
158

159
  if (user == NULL) {
87,752,044✔
160
    if (auth == NULL || strlen(auth) != (TSDB_TOKEN_LEN - 1)) {
3,621✔
161
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_TOKEN);
903✔
162
    }
163
  } else if (!validateUserName(user)) {
87,748,423✔
164
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
165
  }
166
  int32_t code = 0;
87,750,187✔
167

168
  char localDb[TSDB_DB_NAME_LEN] = {0};
87,750,187✔
169
  if (db != NULL && strlen(db) > 0) {
87,750,137✔
170
    if (!validateDbName(db)) {
478,845✔
171
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
172
    }
173

174
    tstrncpy(localDb, db, sizeof(localDb));
479,830✔
175
    (void)strdequote(localDb);
480,229✔
176
  }
177

178
  int32_t totpCode = -1;
87,750,114✔
179
  if (totp != NULL) {
87,750,114✔
180
    char* endptr = NULL;
2,771✔
181
    totpCode = taosStr2Int32(totp, &endptr, 10);
2,771✔
182
    if (endptr == totp || *endptr != '\0' || totpCode < 0 || totpCode > 999999) {
2,771✔
183
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_TOTP_CODE);
×
184
    }
185
  }
186

187
  SCorEpSet epSet = {0};
87,750,114✔
188
  if (ip) {
87,751,548✔
189
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
85,671,198✔
190
  } else {
191
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
2,080,350✔
192
  }
193

194
  if (port) {
87,750,398✔
195
    epSet.epSet.eps[0].port = port;
84,807,315✔
196
    epSet.epSet.eps[1].port = port;
84,807,315✔
197
  }
198

199
  char* key = getClusterKey(user, auth, ip, port);
87,750,398✔
200
  if (NULL == key) {
87,752,966✔
201
    TSC_ERR_RET(terrno);
×
202
  }
203
  tscInfo("connecting to server, numOfEps:%d inUse:%d user:%s db:%s key:%s", epSet.epSet.numOfEps, epSet.epSet.inUse,
87,752,966✔
204
          user ? user : "", db, key);
205
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
177,587,813✔
206
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
89,833,903✔
207
  }
208

209
  SAppInstInfo** pInst = NULL;
87,753,910✔
210
  code = taosThreadMutexLock(&appInfo.mutex);
87,753,910✔
211
  if (TSDB_CODE_SUCCESS != code) {
87,753,507✔
212
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
213
    TSC_ERR_RET(code);
×
214
  }
215

216
  pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
87,753,507✔
217
  SAppInstInfo* p = NULL;
87,753,507✔
218
  if (pInst == NULL) {
87,753,507✔
219
    p = taosMemoryCalloc(1, sizeof(struct SAppInstInfo));
1,706,398✔
220
    if (NULL == p) {
1,706,398✔
221
      TSC_ERR_JRET(terrno);
×
222
    }
223
    p->mgmtEp = epSet;
1,706,398✔
224
    code = taosThreadMutexInit(&p->qnodeMutex, NULL);
1,706,398✔
225
    if (TSDB_CODE_SUCCESS != code) {
1,706,398✔
226
      taosMemoryFree(p);
×
227
      TSC_ERR_JRET(code);
×
228
    }
229
    code = openTransporter(user, auth, tsNumOfCores / 2, &p->pTransporter);
1,706,398✔
230
    if (TSDB_CODE_SUCCESS != code) {
1,706,398✔
231
      taosMemoryFree(p);
188✔
232
      TSC_ERR_JRET(code);
188✔
233
    }
234
    code = appHbMgrInit(p, key, &p->pAppHbMgr);
1,706,210✔
235
    if (TSDB_CODE_SUCCESS != code) {
1,706,210✔
236
      destroyAppInst(&p);
×
237
      TSC_ERR_JRET(code);
×
238
    }
239
    code = taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
1,706,210✔
240
    if (TSDB_CODE_SUCCESS != code) {
1,706,210✔
241
      destroyAppInst(&p);
×
242
      TSC_ERR_JRET(code);
×
243
    }
244
    p->instKey = key;
1,706,210✔
245
    key = NULL;
1,706,210✔
246
    tscInfo("new app inst mgr:%p, user:%s, ip:%s, port:%d", p, user ? user : "", epSet.epSet.eps[0].fqdn,
1,706,210✔
247
            epSet.epSet.eps[0].port);
248

249
    pInst = &p;
1,706,210✔
250
  } else {
251
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
86,047,109✔
252
      tscError("*pInst:%p, pAppHgMgr:%p", *pInst, (*pInst) ? (*pInst)->pAppHbMgr : NULL);
×
253
      TSC_ERR_JRET(TSDB_CODE_TSC_INTERNAL_ERROR);
×
254
    }
255
    // reset to 0 in case of conn with duplicated user key but its user has ever been dropped.
256
    atomic_store_8(&(*pInst)->pAppHbMgr->connHbFlag, 0);
86,047,109✔
257
  }
258

259
_return:
87,753,507✔
260

261
  if (TSDB_CODE_SUCCESS != code) {
87,753,507✔
262
    (void)taosThreadMutexUnlock(&appInfo.mutex);
188✔
263
    taosMemoryFreeClear(key);
188✔
264
    return code;
188✔
265
  } else {
266
    code = taosThreadMutexUnlock(&appInfo.mutex);
87,753,319✔
267
    taosMemoryFreeClear(key);
87,752,317✔
268
    if (TSDB_CODE_SUCCESS != code) {
87,753,319✔
269
      tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
270
      return code;
×
271
    }
272
    return taosConnectImpl(user, auth, totpCode, localDb, NULL, NULL, *pInst, connType, pObj);
87,753,319✔
273
  }
274
}
275

276
int32_t taos_connect_internal(const char* ip, const char* user, const char* pass, const char* totp, const char* db,
87,747,941✔
277
                              uint16_t port, int connType, STscObj** pObj) {
278
  char auth[TSDB_PASSWORD_LEN + 1] = {0};
87,747,941✔
279
  if (!validatePassword(pass)) {
87,747,941✔
280
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_PASS_LENGTH);
×
281
  }
282

283
  taosEncryptPass_c((uint8_t*)pass, strlen(pass), auth);
87,750,708✔
284
  return taos_connect_by_auth(ip, user, auth, totp, db, port, connType, pObj);
87,749,373✔
285
}
286

287
// SAppInstInfo* getAppInstInfo(const char* clusterKey) {
288
//   SAppInstInfo** ppAppInstInfo = taosHashGet(appInfo.pInstMap, clusterKey, strlen(clusterKey));
289
//   if (ppAppInstInfo != NULL && *ppAppInstInfo != NULL) {
290
//     return *ppAppInstInfo;
291
//   } else {
292
//     return NULL;
293
//   }
294
// }
295

296
void freeQueryParam(SSyncQueryParam* param) {
570,059✔
297
  if (param == NULL) return;
570,059✔
298
  if (TSDB_CODE_SUCCESS != tsem_destroy(&param->sem)) {
570,059✔
299
    tscError("failed to destroy semaphore in freeQueryParam");
×
300
  }
301
  taosMemoryFree(param);
570,059✔
302
}
303

304
int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql,
1,181,956,781✔
305
                     SRequestObj** pRequest, int64_t reqid) {
306
  int32_t code = createRequest(connId, TSDB_SQL_SELECT, reqid, pRequest);
1,181,956,781✔
307
  if (TSDB_CODE_SUCCESS != code) {
1,181,944,377✔
308
    tscError("failed to malloc sqlObj, %s", sql);
2,961✔
309
    return code;
2,961✔
310
  }
311

312
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
1,181,941,416✔
313
  if ((*pRequest)->sqlstr == NULL) {
1,181,944,777✔
314
    tscError("req:0x%" PRIx64 ", failed to prepare sql string buffer, %s", (*pRequest)->self, sql);
×
315
    destroyRequest(*pRequest);
×
316
    *pRequest = NULL;
×
317
    return terrno;
×
318
  }
319

320
  (void)strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen);
1,181,962,536✔
321
  (*pRequest)->sqlstr[sqlLen] = 0;
1,182,016,948✔
322
  (*pRequest)->sqlLen = sqlLen;
1,182,022,223✔
323
  (*pRequest)->validateOnly = validateSql;
1,182,025,664✔
324
  (*pRequest)->stmtBindVersion = 0;
1,182,022,022✔
325

326
  code = sqlSecurityCheckStringLevel(*pRequest, (*pRequest)->sqlstr, (*pRequest)->sqlLen);
1,181,999,021✔
327
  if (code != TSDB_CODE_SUCCESS) {
1,181,928,417✔
328
    tscWarn("req:0x%" PRIx64 ", sql security string check failed, QID:0x%" PRIx64 ", code:%s", (*pRequest)->self,
1,694✔
329
            (*pRequest)->requestId, tstrerror(code));
330
    destroyRequest(*pRequest);
1,694✔
331
    *pRequest = NULL;
1,694✔
332
    return code;
1,694✔
333
  }
334

335
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
1,181,926,723✔
336

337
  STscObj* pTscObj = (*pRequest)->pTscObj;
1,182,002,342✔
338
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
1,181,971,452✔
339
                             sizeof((*pRequest)->self));
340
  if (err) {
1,181,969,103✔
341
    tscError("req:0x%" PRId64 ", failed to add to request container, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
342
             (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
343
    destroyRequest(*pRequest);
×
344
    *pRequest = NULL;
×
345
    return terrno;
×
346
  }
347

348
  (*pRequest)->allocatorRefId = -1;
1,181,969,103✔
349
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
1,181,965,615✔
350
    if (TSDB_CODE_SUCCESS !=
479,932,353✔
351
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
479,922,248✔
352
      tscError("req:0x%" PRId64 ", failed to create node allocator, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
353
               (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
354
      destroyRequest(*pRequest);
×
355
      *pRequest = NULL;
×
356
      return terrno;
×
357
    }
358
  }
359

360
  tscDebug("req:0x%" PRIx64 ", build request, QID:0x%" PRIx64, (*pRequest)->self, (*pRequest)->requestId);
1,182,050,557✔
361
  return TSDB_CODE_SUCCESS;
1,181,918,434✔
362
}
363

364
int32_t buildPreviousRequest(SRequestObj* pRequest, const char* sql, SRequestObj** pNewRequest) {
×
365
  int32_t code =
366
      buildRequest(pRequest->pTscObj->id, sql, strlen(sql), pRequest, pRequest->validateOnly, pNewRequest, 0);
×
367
  if (TSDB_CODE_SUCCESS == code) {
×
368
    pRequest->relation.prevRefId = (*pNewRequest)->self;
×
369
    (*pNewRequest)->relation.nextRefId = pRequest->self;
×
370
    (*pNewRequest)->relation.userRefId = pRequest->self;
×
371
    (*pNewRequest)->isSubReq = true;
×
372
  }
373
  return code;
×
374
}
375

376
int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtCallback* pStmtCb) {
7,569,343✔
377
  STscObj* pTscObj = pRequest->pTscObj;
7,569,343✔
378

379
  SParseContext cxt = {
7,571,608✔
380
      .requestId = pRequest->requestId,
7,570,555✔
381
      .requestRid = pRequest->self,
7,569,383✔
382
      .acctId = pTscObj->acctId,
7,569,359✔
383
      .db = pRequest->pDb,
7,568,341✔
384
      .topicQuery = topicQuery,
385
      .pSql = pRequest->sqlstr,
7,571,426✔
386
      .sqlLen = pRequest->sqlLen,
7,572,595✔
387
      .pMsg = pRequest->msgBuf,
7,570,704✔
388
      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
389
      .pTransporter = pTscObj->pAppInfo->pTransporter,
7,568,429✔
390
      .pStmtCb = pStmtCb,
391
      .pUser = pTscObj->user,
7,569,954✔
392
      .userId = pTscObj->userId,
7,566,560✔
393
      .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
7,570,329✔
394
      .enableSysInfo = pTscObj->sysInfo,
7,570,260✔
395
      .minSecLevel = pTscObj->minSecLevel,
7,569,726✔
396
      .maxSecLevel = pTscObj->maxSecLevel,
7,570,836✔
397
      .macMode = pTscObj->pAppInfo->serverCfg.macActive,  // propagates cluster-level MAC state into parser/executor
7,568,837✔
398
      .sodInitial = pTscObj->pAppInfo->serverCfg.sodInitial,
7,567,754✔
399
      .svrVer = pTscObj->sVer,
7,571,787✔
400
      .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
7,569,973✔
401
      .stmtBindVersion = pRequest->stmtBindVersion,
7,571,368✔
402
      .setQueryFp = setQueryRequest,
403
      .timezone = pTscObj->optionInfo.timezone,
7,568,690✔
404
      .charsetCxt = pTscObj->optionInfo.charsetCxt,
7,570,006✔
405
  };
406

407
  cxt.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
7,569,114✔
408
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.pCatalog);
7,572,633✔
409
  if (code != TSDB_CODE_SUCCESS) {
7,569,452✔
410
    return code;
×
411
  }
412

413
  code = qParseSql(&cxt, pQuery);
7,569,452✔
414
  if (TSDB_CODE_SUCCESS == code) {
7,560,237✔
415
    if ((*pQuery)->haveResultSet) {
7,559,100✔
416
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols,
437✔
417
                              (*pQuery)->pResExtSchema, pRequest->stmtBindVersion > 0);
437✔
418
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
437✔
419
    }
420
  }
421

422
  if (TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) {
7,561,650✔
423
    TSWAP(pRequest->dbList, (*pQuery)->pDbList);
7,556,327✔
424
    TSWAP(pRequest->tableList, (*pQuery)->pTableList);
7,562,539✔
425
    TSWAP(pRequest->targetTableList, (*pQuery)->pTargetTableList);
7,564,389✔
426
  }
427

428
  taosArrayDestroy(cxt.pTableMetaPos);
7,557,199✔
429
  taosArrayDestroy(cxt.pTableVgroupPos);
7,551,121✔
430

431
  return code;
7,555,447✔
432
}
433
#ifdef TD_ENTERPRISE
434
static uint8_t getShowVarPrivMask(SRequestObj* pRequest) {
17,650✔
435
  SCatalog*        pCatalog = NULL;
17,650✔
436
  SGetUserAuthRsp  authRsp = {0};
17,650✔
437
  STscObj*         pTscObj = pRequest->pTscObj;
17,650✔
438
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
17,650✔
439
                           .requestId = pRequest->requestId,
17,650✔
440
                           .requestObjRefId = pRequest->self,
17,650✔
441
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
17,650✔
442

443
  if (TSDB_CODE_SUCCESS != catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog)) {
17,650✔
444
    return 0;
×
445
  }
446
  if (TSDB_CODE_SUCCESS != catalogGetUserAuth(pCatalog, &conn, pTscObj->user, &authRsp)) {
17,650✔
447
    return 0;
×
448
  }
449

450
  uint8_t mask = 0;
17,650✔
451
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_SYSTEM_SHOW)) mask |= SHOW_VAR_PRIV_SYSTEM;
17,650✔
452
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_SECURITY_SHOW)) mask |= SHOW_VAR_PRIV_SECURITY;
17,650✔
453
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_AUDIT_SHOW)) mask |= SHOW_VAR_PRIV_AUDIT;
17,650✔
454
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_DEBUG_SHOW)) mask |= SHOW_VAR_PRIV_DEBUG;
17,650✔
455
  return mask;
17,650✔
456
}
457
#endif
458

459
int32_t execLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
×
460
  SRetrieveTableRsp* pRsp = NULL;
×
461
  int8_t             biMode = atomic_load_8(&pRequest->pTscObj->biMode);
×
462
  uint8_t            showVarPrivMask = SHOW_VAR_PRIV_ALL;
×
463
#ifdef TD_ENTERPRISE
464
  if (pQuery->pRoot != NULL && nodeType(pQuery->pRoot) == QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT) {
×
465
    showVarPrivMask = getShowVarPrivMask(pRequest);
×
466
  }
467
#endif
468
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, showVarPrivMask, pQuery->pRoot, &pRsp,
×
469
                              biMode, pRequest->pTscObj->optionInfo.charsetCxt);
×
470
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
×
471
    code = setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4,
×
472
                                 pRequest->stmtBindVersion > 0);
×
473
  }
474

475
  return code;
×
476
}
477

478
int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
462,003✔
479
  // drop table if exists not_exists_table
480
  if (NULL == pQuery->pCmdMsg) {
462,003✔
481
    return TSDB_CODE_SUCCESS;
×
482
  }
483

484
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
462,003✔
485
  pRequest->type = pMsgInfo->msgType;
462,003✔
486
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
462,003✔
487
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
462,003✔
488

489
  STscObj*      pTscObj = pRequest->pTscObj;
462,003✔
490
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
462,003✔
491

492
  // int64_t transporterId = 0;
493
  TSC_ERR_RET(asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg));
462,003✔
494
  TSC_ERR_RET(tsem_wait(&pRequest->body.rspSem));
462,015✔
495
  return TSDB_CODE_SUCCESS;
462,015✔
496
}
497

498
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
2,006,030,800✔
499

500
void asyncExecLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
6,659,129✔
501
  SRetrieveTableRsp* pRsp = NULL;
6,659,129✔
502
  if (pRequest->validateOnly) {
6,659,129✔
503
    doRequestCallback(pRequest, 0);
11,394✔
504
    return;
11,394✔
505
  }
506

507
  uint8_t showVarPrivMask = SHOW_VAR_PRIV_ALL;
6,647,735✔
508
#ifdef TD_ENTERPRISE
509
  if (pQuery->pRoot != NULL && nodeType(pQuery->pRoot) == QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT) {
6,647,735✔
510
    showVarPrivMask = getShowVarPrivMask(pRequest);
17,650✔
511
  }
512
#endif
513
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, showVarPrivMask, pQuery->pRoot, &pRsp,
13,257,959✔
514
                              atomic_load_8(&pRequest->pTscObj->biMode), pRequest->pTscObj->optionInfo.charsetCxt);
13,257,903✔
515
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
6,647,735✔
516
    code = setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4,
3,707,520✔
517
                                 pRequest->stmtBindVersion > 0);
3,707,520✔
518
  }
519

520
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
6,647,735✔
521
  pRequest->code = code;
6,647,735✔
522

523
  if (pRequest->code != TSDB_CODE_SUCCESS) {
6,647,735✔
524
    pResultInfo->numOfRows = 0;
2,080✔
525
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
2,080✔
526
             pRequest->requestId);
527
  } else {
528
    tscDebug(
6,645,655✔
529
        "req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
530
        pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed, pRequest->requestId);
531
  }
532

533
  doRequestCallback(pRequest, code);
6,647,735✔
534
}
535

536
int32_t asyncExecDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
103,881,418✔
537
  if (pRequest->validateOnly) {
103,881,418✔
538
    doRequestCallback(pRequest, 0);
×
539
    return TSDB_CODE_SUCCESS;
×
540
  }
541

542
  // drop table if exists not_exists_table
543
  if (NULL == pQuery->pCmdMsg) {
103,881,529✔
544
    doRequestCallback(pRequest, 0);
7,870✔
545
    return TSDB_CODE_SUCCESS;
7,870✔
546
  }
547

548
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
103,873,639✔
549
  // Clear pQuery->pCmdMsg before the async call so that nodesDestroyNode (which may be
550
  // triggered by the async response callback on another thread) will not double-free pCmdMsg.
551
  pQuery->pCmdMsg = NULL;
103,873,669✔
552
  pRequest->type = pMsgInfo->msgType;
103,873,518✔
553
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
103,873,463✔
554
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
103,873,215✔
555

556
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
103,872,353✔
557
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
103,871,737✔
558

559
  int32_t code = asyncSendMsgToServer(pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg);
103,878,695✔
560
  // pMsgInfo->pMsg has been transferred to pRequest->body.requestMsg and pMsgInfo->epSet has
561
  // been consumed by asyncSendMsgToServer; the SCmdMsgInfo struct itself is no longer needed.
562
  // Use the local pMsgInfo variable (not pQuery->pCmdMsg) to avoid a use-after-free: the async
563
  // response callback may have run on another thread and destroyed pQuery by this point.
564
  taosMemoryFree(pMsgInfo);
103,880,151✔
565
  if (code) {
103,882,371✔
566
    doRequestCallback(pRequest, code);
×
567
  }
568
  return code;
103,883,041✔
569
}
570

571
int compareQueryNodeLoad(const void* elem1, const void* elem2) {
178,800✔
572
  SQueryNodeLoad* node1 = (SQueryNodeLoad*)elem1;
178,800✔
573
  SQueryNodeLoad* node2 = (SQueryNodeLoad*)elem2;
178,800✔
574

575
  if (node1->load < node2->load) {
178,800✔
576
    return -1;
×
577
  }
578

579
  return node1->load > node2->load;
178,800✔
580
}
581

582
int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) {
381,427✔
583
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
381,427✔
584
  if (pInfo->pQnodeList) {
381,427✔
585
    taosArrayDestroy(pInfo->pQnodeList);
375,855✔
586
    pInfo->pQnodeList = NULL;
375,855✔
587
    tscDebug("QnodeList cleared in cluster 0x%" PRIx64, pInfo->clusterId);
375,855✔
588
  }
589

590
  if (pNodeList) {
381,427✔
591
    pInfo->pQnodeList = taosArrayDup(pNodeList, NULL);
381,427✔
592
    taosArraySort(pInfo->pQnodeList, compareQueryNodeLoad);
381,427✔
593
    tscDebug("QnodeList updated in cluster 0x%" PRIx64 ", num:%ld", pInfo->clusterId,
381,427✔
594
             taosArrayGetSize(pInfo->pQnodeList));
595
  }
596
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
381,427✔
597

598
  return TSDB_CODE_SUCCESS;
381,427✔
599
}
600

601
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
1,175,197,542✔
602
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
1,175,197,542✔
603
    *required = false;
1,161,677,728✔
604
    return TSDB_CODE_SUCCESS;
1,161,680,093✔
605
  }
606

607
  int32_t       code = TSDB_CODE_SUCCESS;
13,519,814✔
608
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
13,519,814✔
609
  *required = false;
13,520,368✔
610

611
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
13,520,368✔
612
  *required = (NULL == pInfo->pQnodeList);
13,520,368✔
613
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
13,520,368✔
614
  return TSDB_CODE_SUCCESS;
13,520,368✔
615
}
616

617
int32_t getQnodeList(SRequestObj* pRequest, SArray** pNodeList) {
×
618
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
×
619
  int32_t       code = 0;
×
620

621
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
×
622
  if (pInfo->pQnodeList) {
×
623
    *pNodeList = taosArrayDup(pInfo->pQnodeList, NULL);
×
624
  }
625
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
×
626
  if (NULL == *pNodeList) {
×
627
    SCatalog* pCatalog = NULL;
×
628
    code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
×
629
    if (TSDB_CODE_SUCCESS == code) {
×
630
      *pNodeList = taosArrayInit(5, sizeof(SQueryNodeLoad));
×
631
      if (NULL == pNodeList) {
×
632
        TSC_ERR_RET(terrno);
×
633
      }
634
      SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
×
635
                               .requestId = pRequest->requestId,
×
636
                               .requestObjRefId = pRequest->self,
×
637
                               .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
×
638
      code = catalogGetQnodeList(pCatalog, &conn, *pNodeList);
×
639
    }
640

641
    if (TSDB_CODE_SUCCESS == code && *pNodeList) {
×
642
      code = updateQnodeList(pInfo, *pNodeList);
×
643
    }
644
  }
645

646
  return code;
×
647
}
648

649
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList) {
8,929,482✔
650
  pRequest->type = pQuery->msgType;
8,929,482✔
651
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
8,927,536✔
652

653
  SPlanContext cxt = {.queryId = pRequest->requestId,
9,292,316✔
654
                      .acctId = pRequest->pTscObj->acctId,
8,927,922✔
655
                      .mgmtEpSet = getEpSet_s(&pAppInfo->mgmtEp),
8,919,535✔
656
                      .pAstRoot = pQuery->pRoot,
8,931,677✔
657
                      .showRewrite = pQuery->showRewrite,
8,930,983✔
658
                      .pMsg = pRequest->msgBuf,
8,930,969✔
659
                      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
660
                      .pUser = pRequest->pTscObj->user,
8,927,254✔
661
                      .userId = pRequest->pTscObj->userId,
8,917,825✔
662
                      .timezone = pRequest->pTscObj->optionInfo.timezone,
8,928,831✔
663
                      .sysInfo = pRequest->pTscObj->sysInfo,
8,925,901✔
664
                      .minSecLevel = pRequest->pTscObj->minSecLevel,
8,930,969✔
665
                      .maxSecLevel = pRequest->pTscObj->maxSecLevel,
8,923,657✔
666
                      .macMode = pAppInfo->serverCfg.macActive};
8,929,100✔
667

668
  return qCreateQueryPlan(&cxt, pPlan, pNodeList);
8,920,650✔
669
}
670

671
int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols,
292,544,245✔
672
                         const SExtSchema* pExtSchema, bool isStmt) {
673
  if (pResInfo == NULL || pSchema == NULL || numOfCols <= 0) {
292,544,245✔
674
    tscError("invalid paras, pResInfo == NULL || pSchema == NULL || numOfCols <= 0");
×
675
    return TSDB_CODE_INVALID_PARA;
×
676
  }
677

678
  pResInfo->numOfCols = numOfCols;
292,559,197✔
679
  if (pResInfo->fields != NULL) {
292,559,755✔
680
    taosMemoryFree(pResInfo->fields);
16,190✔
681
  }
682
  if (pResInfo->userFields != NULL) {
292,557,244✔
683
    taosMemoryFree(pResInfo->userFields);
16,190✔
684
  }
685
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
292,558,711✔
686
  if (NULL == pResInfo->fields) return terrno;
292,549,269✔
687
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
292,549,124✔
688
  if (NULL == pResInfo->userFields) {
292,546,133✔
689
    taosMemoryFree(pResInfo->fields);
×
690
    return terrno;
×
691
  }
692
  if (numOfCols != pResInfo->numOfCols) {
292,547,004✔
693
    tscError("numOfCols:%d != pResInfo->numOfCols:%d", numOfCols, pResInfo->numOfCols);
×
694
    return TSDB_CODE_FAILED;
×
695
  }
696

697
  for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
1,505,674,823✔
698
    pResInfo->fields[i].type = pSchema[i].type;
1,213,112,083✔
699

700
    pResInfo->userFields[i].type = pSchema[i].type;
1,213,118,068✔
701
    // userFields must convert to type bytes, no matter isStmt or not
702
    pResInfo->userFields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, false);
1,213,121,565✔
703
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
1,213,113,372✔
704
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
1,213,112,044✔
705
      decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale);
1,573,797✔
706
    }
707

708
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
1,213,111,123✔
709
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
1,213,130,818✔
710
  }
711
  return TSDB_CODE_SUCCESS;
292,567,313✔
712
}
713

714
void setResPrecision(SReqResultInfo* pResInfo, int32_t precision) {
210,532,647✔
715
  if (precision != TSDB_TIME_PRECISION_MILLI && precision != TSDB_TIME_PRECISION_MICRO &&
210,532,647✔
716
      precision != TSDB_TIME_PRECISION_NANO) {
717
    return;
×
718
  }
719

720
  pResInfo->precision = precision;
210,532,647✔
721
}
722

723
int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) {
215,076,196✔
724
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
215,076,196✔
725
  if (NULL == nodeList) {
215,092,151✔
726
    return terrno;
×
727
  }
728
  char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client";
215,097,394✔
729

730
  int32_t dbNum = taosArrayGetSize(pDbVgList);
215,097,394✔
731
  for (int32_t i = 0; i < dbNum; ++i) {
427,465,785✔
732
    SArray* pVg = taosArrayGetP(pDbVgList, i);
212,355,260✔
733
    if (NULL == pVg) {
212,360,103✔
734
      continue;
×
735
    }
736
    int32_t vgNum = taosArrayGetSize(pVg);
212,360,103✔
737
    if (vgNum <= 0) {
212,361,158✔
738
      continue;
664,282✔
739
    }
740

741
    for (int32_t j = 0; j < vgNum; ++j) {
741,238,615✔
742
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
529,539,179✔
743
      if (NULL == pInfo) {
529,555,135✔
744
        taosArrayDestroy(nodeList);
×
745
        return TSDB_CODE_OUT_OF_RANGE;
×
746
      }
747
      SQueryNodeLoad load = {0};
529,555,135✔
748
      load.addr.nodeId = pInfo->vgId;
529,546,904✔
749
      load.addr.epSet = pInfo->epSet;
529,568,104✔
750

751
      if (NULL == taosArrayPush(nodeList, &load)) {
529,493,123✔
752
        taosArrayDestroy(nodeList);
×
753
        return terrno;
×
754
      }
755
    }
756
  }
757

758
  int32_t vnodeNum = taosArrayGetSize(nodeList);
215,110,525✔
759
  if (vnodeNum > 0) {
215,113,437✔
760
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
211,355,359✔
761
    goto _return;
211,350,805✔
762
  }
763

764
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
3,758,078✔
765
  if (mnodeNum <= 0) {
3,757,016✔
766
    tscDebug("0x%" PRIx64 " %s policy, empty node list", pRequest->requestId, policy);
×
767
    goto _return;
×
768
  }
769

770
  void* pData = taosArrayGet(pMnodeList, 0);
3,757,016✔
771
  if (NULL == pData) {
3,757,016✔
772
    taosArrayDestroy(nodeList);
×
773
    return TSDB_CODE_OUT_OF_RANGE;
×
774
  }
775
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
3,757,016✔
776
    taosArrayDestroy(nodeList);
×
777
    return terrno;
×
778
  }
779

780
  tscDebug("0x%" PRIx64 " %s policy, use mnode list, num:%d", pRequest->requestId, policy, mnodeNum);
3,757,017✔
781

782
_return:
137,651✔
783

784
  *pNodeList = nodeList;
215,104,572✔
785

786
  return TSDB_CODE_SUCCESS;
215,104,007✔
787
}
788

789
int32_t buildQnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pQnodeList) {
1,706,442✔
790
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
1,706,442✔
791
  if (NULL == nodeList) {
1,706,442✔
792
    return terrno;
×
793
  }
794

795
  int32_t qNodeNum = taosArrayGetSize(pQnodeList);
1,706,442✔
796
  if (qNodeNum > 0) {
1,706,442✔
797
    void* pData = taosArrayGet(pQnodeList, 0);
1,682,498✔
798
    if (NULL == pData) {
1,682,498✔
799
      taosArrayDestroy(nodeList);
×
800
      return TSDB_CODE_OUT_OF_RANGE;
×
801
    }
802
    if (NULL == taosArrayAddBatch(nodeList, pData, qNodeNum)) {
1,682,498✔
803
      taosArrayDestroy(nodeList);
×
804
      return terrno;
×
805
    }
806
    tscDebug("0x%" PRIx64 " qnode policy, use qnode list, num:%d", pRequest->requestId, qNodeNum);
1,682,498✔
807
    goto _return;
1,682,498✔
808
  }
809

810
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
23,944✔
811
  if (mnodeNum <= 0) {
23,944✔
812
    tscDebug("0x%" PRIx64 " qnode policy, empty node list", pRequest->requestId);
×
813
    goto _return;
×
814
  }
815

816
  void* pData = taosArrayGet(pMnodeList, 0);
23,944✔
817
  if (NULL == pData) {
23,944✔
818
    taosArrayDestroy(nodeList);
×
819
    return TSDB_CODE_OUT_OF_RANGE;
×
820
  }
821
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
23,944✔
822
    taosArrayDestroy(nodeList);
×
823
    return terrno;
×
824
  }
825

826
  tscDebug("0x%" PRIx64 " qnode policy, use mnode list, num:%d", pRequest->requestId, mnodeNum);
23,944✔
827

828
_return:
×
829

830
  *pNodeList = nodeList;
1,706,442✔
831

832
  return TSDB_CODE_SUCCESS;
1,706,442✔
833
}
834

835
void freeVgList(void* list) {
8,801,451✔
836
  SArray* pList = *(SArray**)list;
8,801,451✔
837
  taosArrayDestroy(pList);
8,804,417✔
838
}
8,789,011✔
839

840
int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SMetaData* pResultMeta) {
207,855,755✔
841
  SArray* pDbVgList = NULL;
207,855,755✔
842
  SArray* pQnodeList = NULL;
207,855,755✔
843
  FDelete fp = NULL;
207,855,755✔
844
  int32_t code = 0;
207,855,755✔
845

846
  switch (tsQueryPolicy) {
207,855,755✔
847
    case QUERY_POLICY_VNODE:
206,163,490✔
848
    case QUERY_POLICY_CLIENT: {
849
      if (pResultMeta) {
206,163,490✔
850
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
206,171,957✔
851
        if (NULL == pDbVgList) {
206,168,275✔
852
          code = terrno;
×
853
          goto _return;
×
854
        }
855
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
206,168,275✔
856
        for (int32_t i = 0; i < dbNum; ++i) {
409,710,690✔
857
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
203,544,299✔
858
          if (pRes->code || NULL == pRes->pRes) {
203,546,852✔
859
            continue;
655✔
860
          }
861

862
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
407,102,070✔
863
            code = terrno;
×
864
            goto _return;
×
865
          }
866
        }
867
      } else {
868
        fp = freeVgList;
1,782✔
869

870
        int32_t dbNum = taosArrayGetSize(pRequest->dbList);
1,782✔
871
        if (dbNum > 0) {
1,782✔
872
          SCatalog*     pCtg = NULL;
1,782✔
873
          SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
1,782✔
874
          code = catalogGetHandle(pInst->clusterId, &pCtg);
1,782✔
875
          if (code != TSDB_CODE_SUCCESS) {
1,782✔
876
            goto _return;
×
877
          }
878

879
          pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
1,782✔
880
          if (NULL == pDbVgList) {
1,782✔
881
            code = terrno;
×
882
            goto _return;
×
883
          }
884
          SArray* pVgList = NULL;
1,782✔
885
          for (int32_t i = 0; i < dbNum; ++i) {
3,564✔
886
            char*            dbFName = taosArrayGet(pRequest->dbList, i);
1,782✔
887
            SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
1,782✔
888
                                     .requestId = pRequest->requestId,
1,782✔
889
                                     .requestObjRefId = pRequest->self,
1,782✔
890
                                     .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
1,782✔
891

892
            // catalogGetDBVgList will handle dbFName == null.
893
            code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
1,782✔
894
            if (code) {
1,782✔
895
              goto _return;
×
896
            }
897

898
            if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
1,782✔
899
              code = terrno;
×
900
              goto _return;
×
901
            }
902
          }
903
        }
904
      }
905

906
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
206,168,173✔
907
      break;
206,164,929✔
908
    }
909
    case QUERY_POLICY_HYBRID:
1,706,442✔
910
    case QUERY_POLICY_QNODE: {
911
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
1,738,142✔
912
        SMetaRes* pRes = taosArrayGet(pResultMeta->pQnodeList, 0);
31,700✔
913
        if (pRes->code) {
31,700✔
914
          pQnodeList = NULL;
×
915
        } else {
916
          pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL);
31,700✔
917
          if (NULL == pQnodeList) {
31,700✔
918
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
919
            goto _return;
×
920
          }
921
        }
922
      } else {
923
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
1,674,742✔
924
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
1,674,742✔
925
        if (pInst->pQnodeList) {
1,674,742✔
926
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
1,674,742✔
927
          if (NULL == pQnodeList) {
1,674,742✔
928
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
929
            goto _return;
×
930
          }
931
        }
932
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
1,674,742✔
933
      }
934

935
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
1,706,442✔
936
      break;
1,706,442✔
937
    }
938
    default:
536✔
939
      tscError("unknown query policy: %d", tsQueryPolicy);
536✔
940
      return TSDB_CODE_APP_ERROR;
×
941
  }
942

943
_return:
207,871,371✔
944
  taosArrayDestroyEx(pDbVgList, fp);
207,871,371✔
945
  taosArrayDestroy(pQnodeList);
207,875,212✔
946

947
  return code;
207,878,473✔
948
}
949

950
int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList) {
8,920,594✔
951
  SArray* pDbVgList = NULL;
8,920,594✔
952
  SArray* pQnodeList = NULL;
8,920,594✔
953
  int32_t code = 0;
8,922,125✔
954

955
  switch (tsQueryPolicy) {
8,922,125✔
956
    case QUERY_POLICY_VNODE:
8,914,761✔
957
    case QUERY_POLICY_CLIENT: {
958
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
8,914,761✔
959
      if (dbNum > 0) {
8,926,731✔
960
        SCatalog*     pCtg = NULL;
8,797,448✔
961
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
8,796,393✔
962
        code = catalogGetHandle(pInst->clusterId, &pCtg);
8,797,788✔
963
        if (code != TSDB_CODE_SUCCESS) {
8,792,973✔
964
          goto _return;
×
965
        }
966

967
        pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
8,792,973✔
968
        if (NULL == pDbVgList) {
8,799,894✔
969
          code = terrno;
×
970
          goto _return;
×
971
        }
972
        SArray* pVgList = NULL;
8,799,930✔
973
        for (int32_t i = 0; i < dbNum; ++i) {
17,598,621✔
974
          char*            dbFName = taosArrayGet(pRequest->dbList, i);
8,797,436✔
975
          SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
8,801,115✔
976
                                   .requestId = pRequest->requestId,
8,801,126✔
977
                                   .requestObjRefId = pRequest->self,
8,798,650✔
978
                                   .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
8,796,134✔
979

980
          // catalogGetDBVgList will handle dbFName == null.
981
          code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
8,805,215✔
982
          if (code) {
8,801,688✔
983
            goto _return;
×
984
          }
985

986
          if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
8,805,205✔
987
            code = terrno;
×
988
            goto _return;
×
989
          }
990
        }
991
      }
992

993
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
8,934,275✔
994
      break;
8,931,397✔
995
    }
996
    case QUERY_POLICY_HYBRID:
×
997
    case QUERY_POLICY_QNODE: {
998
      TSC_ERR_JRET(getQnodeList(pRequest, &pQnodeList));
×
999

1000
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
×
1001
      break;
×
1002
    }
1003
    default:
7,364✔
1004
      tscError("unknown query policy: %d", tsQueryPolicy);
7,364✔
1005
      return TSDB_CODE_APP_ERROR;
×
1006
  }
1007

1008
_return:
8,931,350✔
1009

1010
  taosArrayDestroyEx(pDbVgList, freeVgList);
8,929,489✔
1011
  taosArrayDestroy(pQnodeList);
8,926,580✔
1012

1013
  return code;
8,929,690✔
1014
}
1015

1016
int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) {
8,924,287✔
1017
  void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter;
8,924,287✔
1018

1019
  SExecResult      res = {0};
8,932,782✔
1020
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
8,926,860✔
1021
                           .requestId = pRequest->requestId,
8,928,482✔
1022
                           .requestObjRefId = pRequest->self};
8,927,638✔
1023
  SSchedulerReq    req = {
9,286,228✔
1024
         .syncReq = true,
1025
         .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
8,923,597✔
1026
         .pConn = &conn,
1027
         .pNodeList = pNodeList,
1028
         .pDag = pDag,
1029
         .sql = pRequest->sqlstr,
8,923,597✔
1030
         .startTs = pRequest->metric.start,
8,930,138✔
1031
         .execFp = NULL,
1032
         .cbParam = NULL,
1033
         .chkKillFp = chkRequestKilled,
1034
         .chkKillParam = (void*)pRequest->self,
8,926,772✔
1035
         .pExecRes = &res,
1036
         .source = pRequest->source,
8,922,891✔
1037
         .secureDelete = pRequest->secureDelete,
8,922,378✔
1038
         .pWorkerCb = getTaskPoolWorkerCb(),
8,930,903✔
1039
  };
1040

1041
  int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob);
8,926,014✔
1042

1043
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
8,936,951✔
1044
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
8,937,095✔
1045

1046
  if (code != TSDB_CODE_SUCCESS) {
8,935,333✔
1047
    schedulerFreeJob(&pRequest->body.queryJob, 0);
×
1048

1049
    pRequest->code = code;
×
1050
    terrno = code;
×
1051
    return pRequest->code;
×
1052
  }
1053

1054
  if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_DELETE == pRequest->type ||
8,935,333✔
1055
      TDMT_VND_CREATE_TABLE == pRequest->type) {
100,284✔
1056
    pRequest->body.resInfo.numOfRows = res.numOfRows;
8,896,342✔
1057
    if (TDMT_VND_SUBMIT == pRequest->type) {
8,896,776✔
1058
      STscObj*            pTscObj = pRequest->pTscObj;
8,834,673✔
1059
      SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
8,835,395✔
1060
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows);
8,836,624✔
1061
    }
1062

1063
    schedulerFreeJob(&pRequest->body.queryJob, 0);
8,896,685✔
1064
  }
1065

1066
  pRequest->code = res.code;
8,936,657✔
1067
  terrno = res.code;
8,937,107✔
1068
  return pRequest->code;
8,933,698✔
1069
}
1070

1071
int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
701,875,167✔
1072
  SArray*      pArray = NULL;
701,875,167✔
1073
  SSubmitRsp2* pRsp = (SSubmitRsp2*)res;
701,875,167✔
1074
  if (NULL == pRsp->aCreateTbRsp) {
701,875,167✔
1075
    return TSDB_CODE_SUCCESS;
688,097,274✔
1076
  }
1077

1078
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
13,835,735✔
1079
  for (int32_t i = 0; i < tbNum; ++i) {
31,037,011✔
1080
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
17,202,296✔
1081
    if (pTbRsp->pMeta) {
17,203,695✔
1082
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
16,865,477✔
1083
    }
1084
  }
1085

1086
  return TSDB_CODE_SUCCESS;
13,834,715✔
1087
}
1088

1089
int32_t handleQueryExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
166,099,893✔
1090
  int32_t code = 0;
166,099,893✔
1091
  SArray* pArray = NULL;
166,099,893✔
1092
  SArray* pTbArray = (SArray*)res;
166,099,893✔
1093
  int32_t tbNum = taosArrayGetSize(pTbArray);
166,099,893✔
1094
  if (tbNum <= 0) {
166,101,028✔
1095
    return TSDB_CODE_SUCCESS;
×
1096
  }
1097

1098
  pArray = taosArrayInit(tbNum, sizeof(STbSVersion));
166,101,028✔
1099
  if (NULL == pArray) {
166,101,546✔
1100
    return terrno;
461✔
1101
  }
1102

1103
  for (int32_t i = 0; i < tbNum; ++i) {
540,272,649✔
1104
    STbVerInfo* tbInfo = taosArrayGet(pTbArray, i);
374,170,363✔
1105
    if (NULL == tbInfo) {
374,170,812✔
1106
      code = terrno;
×
1107
      goto _return;
×
1108
    }
1109
    STbSVersion tbSver = {
374,170,812✔
1110
        .tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion, .rver = tbInfo->rversion};
374,170,216✔
1111
    if (NULL == taosArrayPush(pArray, &tbSver)) {
374,171,543✔
1112
      code = terrno;
×
1113
      goto _return;
×
1114
    }
1115
  }
1116

1117
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
166,102,286✔
1118
                           .requestId = pRequest->requestId,
166,102,323✔
1119
                           .requestObjRefId = pRequest->self,
166,102,638✔
1120
                           .mgmtEps = *epset};
1121

1122
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
166,102,408✔
1123

1124
_return:
166,100,319✔
1125

1126
  taosArrayDestroy(pArray);
166,100,642✔
1127
  return code;
166,102,003✔
1128
}
1129

1130
int32_t handleAlterTbExecRes(void* res, SCatalog* pCatalog) {
9,358,705✔
1131
  return catalogUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
9,358,705✔
1132
}
1133

1134
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog) {
78,219,425✔
1135
  return catalogAsyncUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
78,219,425✔
1136
}
1137

1138
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
978,948,883✔
1139
  if (NULL == pRequest->body.resInfo.execRes.res) {
978,948,883✔
1140
    return pRequest->code;
55,121,561✔
1141
  }
1142

1143
  SCatalog*     pCatalog = NULL;
923,803,806✔
1144
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
923,817,253✔
1145

1146
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
923,850,221✔
1147
  if (code) {
923,820,473✔
1148
    return code;
×
1149
  }
1150

1151
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
923,820,473✔
1152
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
923,885,100✔
1153

1154
  switch (pRes->msgType) {
923,889,132✔
1155
    case TDMT_VND_ALTER_TABLE:
4,237,772✔
1156
    case TDMT_MND_ALTER_STB: {
1157
      code = handleAlterTbExecRes(pRes->res, pCatalog);
4,237,772✔
1158
      break;
4,237,772✔
1159
    }
1160
    case TDMT_VND_CREATE_TABLE: {
51,178,666✔
1161
      SArray* pList = (SArray*)pRes->res;
51,178,666✔
1162
      int32_t num = taosArrayGetSize(pList);
51,178,666✔
1163
      for (int32_t i = 0; i < num; ++i) {
110,292,798✔
1164
        void* res = taosArrayGetP(pList, i);
59,108,713✔
1165
        // handleCreateTbExecRes will handle res == null
1166
        code = handleCreateTbExecRes(res, pCatalog);
59,115,840✔
1167
      }
1168
      break;
51,184,085✔
1169
    }
1170
    case TDMT_MND_CREATE_STB: {
433,579✔
1171
      code = handleCreateTbExecRes(pRes->res, pCatalog);
433,579✔
1172
      break;
433,579✔
1173
    }
1174
    case TDMT_VND_SUBMIT: {
701,877,572✔
1175
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
701,877,572✔
1176

1177
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
701,934,250✔
1178
      break;
701,909,922✔
1179
    }
1180
    case TDMT_SCH_QUERY:
166,101,174✔
1181
    case TDMT_SCH_MERGE_QUERY: {
1182
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
166,101,174✔
1183
      break;
166,095,543✔
1184
    }
1185
    default:
792✔
1186
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self,
792✔
1187
               pRequest->type, pRequest->requestId);
1188
      code = TSDB_CODE_APP_ERROR;
×
1189
  }
1190

1191
  return code;
923,860,901✔
1192
}
1193

1194
static bool incompletaFileParsing(SNode* pStmt) {
956,706,294✔
1195
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
956,706,294✔
1196
}
1197

1198
void continuePostSubQuery(SRequestObj* pRequest, SSDataBlock* pBlock) {
×
1199
  SSqlCallbackWrapper* pWrapper = pRequest->pWrapper;
×
1200

1201
  int32_t code = nodesAcquireAllocator(pWrapper->pParseCtx->allocatorId);
×
1202
  if (TSDB_CODE_SUCCESS == code) {
×
1203
    int64_t analyseStart = taosGetTimestampUs();
×
1204
    code = qContinueParsePostQuery(pWrapper->pParseCtx, pRequest->pQuery, pBlock);
×
1205
    pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
×
1206
  }
1207

1208
  if (TSDB_CODE_SUCCESS == code) {
×
1209
    code = qContinuePlanPostQuery(pRequest->pPostPlan);
×
1210
  }
1211

1212
  code = nodesReleaseAllocator(pWrapper->pParseCtx->allocatorId);
×
1213
  handleQueryAnslyseRes(pWrapper, NULL, code);
×
1214
}
×
1215

1216
void returnToUser(SRequestObj* pRequest) {
75,556,456✔
1217
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
75,556,456✔
1218
    // return to client
1219
    doRequestCallback(pRequest, pRequest->code);
75,556,456✔
1220
    return;
75,553,299✔
1221
  }
1222

1223
  SRequestObj* pUserReq = acquireRequest(pRequest->relation.userRefId);
×
1224
  if (pUserReq) {
×
1225
    pUserReq->code = pRequest->code;
×
1226
    // return to client
1227
    doRequestCallback(pUserReq, pUserReq->code);
×
1228
    (void)releaseRequest(pRequest->relation.userRefId);
×
1229
    return;
×
1230
  } else {
1231
    tscError("req:0x%" PRIx64 ", user ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1232
             pRequest->relation.userRefId, pRequest->requestId);
1233
  }
1234
}
1235

1236
static int32_t createResultBlock(TAOS_RES* pRes, int32_t numOfRows, SSDataBlock** pBlock) {
×
1237
  int64_t     lastTs = 0;
×
1238
  TAOS_FIELD* pResFields = taos_fetch_fields(pRes);
×
1239
  int32_t     numOfFields = taos_num_fields(pRes);
×
1240

1241
  int32_t code = createDataBlock(pBlock);
×
1242
  if (code) {
×
1243
    return code;
×
1244
  }
1245

1246
  for (int32_t i = 0; i < numOfFields; ++i) {
×
1247
    SColumnInfoData colInfoData = createColumnInfoData(pResFields[i].type, pResFields[i].bytes, i + 1);
×
1248
    code = blockDataAppendColInfo(*pBlock, &colInfoData);
×
1249
    if (TSDB_CODE_SUCCESS != code) {
×
1250
      blockDataDestroy(*pBlock);
×
1251
      return code;
×
1252
    }
1253
  }
1254

1255
  code = blockDataEnsureCapacity(*pBlock, numOfRows);
×
1256
  if (TSDB_CODE_SUCCESS != code) {
×
1257
    blockDataDestroy(*pBlock);
×
1258
    return code;
×
1259
  }
1260

1261
  for (int32_t i = 0; i < numOfRows; ++i) {
×
1262
    TAOS_ROW pRow = taos_fetch_row(pRes);
×
1263
    if (NULL == pRow[0] || NULL == pRow[1] || NULL == pRow[2]) {
×
1264
      tscError("invalid data from vnode");
×
1265
      blockDataDestroy(*pBlock);
×
1266
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1267
    }
1268
    int64_t ts = *(int64_t*)pRow[0];
×
1269
    if (lastTs < ts) {
×
1270
      lastTs = ts;
×
1271
    }
1272

1273
    for (int32_t j = 0; j < numOfFields; ++j) {
×
1274
      SColumnInfoData* pColInfoData = taosArrayGet((*pBlock)->pDataBlock, j);
×
1275
      code = colDataSetVal(pColInfoData, i, pRow[j], false);
×
1276
      if (TSDB_CODE_SUCCESS != code) {
×
1277
        blockDataDestroy(*pBlock);
×
1278
        return code;
×
1279
      }
1280
    }
1281

1282
    tscInfo("[create stream with histroy] lastKey:%" PRId64 " vgId:%d, vgVer:%" PRId64, ts, *(int32_t*)pRow[1],
×
1283
            *(int64_t*)pRow[2]);
1284
  }
1285

1286
  (*pBlock)->info.window.ekey = lastTs;
×
1287
  (*pBlock)->info.rows = numOfRows;
×
1288

1289
  tscInfo("[create stream with histroy] lastKey:%" PRId64 " numOfRows:%d from all vgroups", lastTs, numOfRows);
×
1290
  return TSDB_CODE_SUCCESS;
×
1291
}
1292

1293
void postSubQueryFetchCb(void* param, TAOS_RES* res, int32_t rowNum) {
×
1294
  SRequestObj* pRequest = (SRequestObj*)res;
×
1295
  if (pRequest->code) {
×
1296
    returnToUser(pRequest);
×
1297
    return;
×
1298
  }
1299

1300
  SSDataBlock* pBlock = NULL;
×
1301
  pRequest->code = createResultBlock(res, rowNum, &pBlock);
×
1302
  if (TSDB_CODE_SUCCESS != pRequest->code) {
×
1303
    tscError("req:0x%" PRIx64 ", create result block failed, QID:0x%" PRIx64 " %s", pRequest->self, pRequest->requestId,
×
1304
             tstrerror(pRequest->code));
1305
    returnToUser(pRequest);
×
1306
    return;
×
1307
  }
1308

1309
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
×
1310
  if (pNextReq) {
×
1311
    continuePostSubQuery(pNextReq, pBlock);
×
1312
    (void)releaseRequest(pRequest->relation.nextRefId);
×
1313
  } else {
1314
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1315
             pRequest->relation.nextRefId, pRequest->requestId);
1316
  }
1317

1318
  blockDataDestroy(pBlock);
×
1319
}
1320

1321
void handlePostSubQuery(SSqlCallbackWrapper* pWrapper) {
×
1322
  SRequestObj* pRequest = pWrapper->pRequest;
×
1323
  if (TD_RES_QUERY(pRequest)) {
×
1324
    taosAsyncFetchImpl(pRequest, postSubQueryFetchCb, pWrapper);
×
1325
    return;
×
1326
  }
1327

1328
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
×
1329
  if (pNextReq) {
×
1330
    continuePostSubQuery(pNextReq, NULL);
×
1331
    (void)releaseRequest(pRequest->relation.nextRefId);
×
1332
  } else {
1333
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1334
             pRequest->relation.nextRefId, pRequest->requestId);
1335
  }
1336
}
1337

1338
// todo refacto the error code  mgmt
1339
void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) {
969,661,254✔
1340
  SSqlCallbackWrapper* pWrapper = param;
969,661,254✔
1341
  SRequestObj*         pRequest = pWrapper->pRequest;
969,661,254✔
1342
  STscObj*             pTscObj = pRequest->pTscObj;
969,698,939✔
1343

1344
  // Note: This is EXECUTE completion callback, not FETCH callback.
1345
  // Scheduler job phase is authoritative. Client phase is only fallback.
1346
  // Let heartbeat read scheduler job phase via schedulerGetJobPhase().
1347

1348
  pRequest->code = code;
969,706,898✔
1349
  if (pResult) {
969,708,016✔
1350
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
969,604,280✔
1351
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
969,599,215✔
1352
  }
1353

1354
  int32_t type = pRequest->type;
969,639,395✔
1355
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
969,615,000✔
1356
    if (pResult) {
748,481,489✔
1357
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
748,460,318✔
1358

1359
      // record the insert rows
1360
      if (TDMT_VND_SUBMIT == type) {
748,502,506✔
1361
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
693,703,103✔
1362
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
693,715,977✔
1363
      }
1364
    }
1365
    schedulerFreeJob(&pRequest->body.queryJob, 0);
748,524,570✔
1366
  }
1367

1368
  taosMemoryFree(pResult);
969,701,957✔
1369
  tscDebug("req:0x%" PRIx64 ", enter scheduler exec cb, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
969,638,945✔
1370
           pRequest->requestId);
1371

1372
  if (code != TSDB_CODE_SUCCESS && NEED_CLIENT_HANDLE_ERROR(code) && pRequest->sqlstr != NULL &&
969,645,650✔
1373
      pRequest->stmtBindVersion == 0) {
85,187✔
1374
    tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%s, tryCount:%d, QID:0x%" PRIx64,
84,791✔
1375
             pRequest->self, tstrerror(code), pRequest->retry, pRequest->requestId);
1376
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
84,791✔
1377
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1378
    }
1379
    restartAsyncQuery(pRequest, code);
84,791✔
1380
    return;
84,791✔
1381
  }
1382

1383
  tscTrace("req:0x%" PRIx64 ", scheduler exec cb, request type:%s", pRequest->self, TMSG_INFO(pRequest->type));
969,560,859✔
1384
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
969,560,859✔
1385
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
3,861,277✔
1386
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1387
    }
1388
  }
1389

1390
  pRequest->metric.execCostUs = taosGetTimestampUs() - pRequest->metric.execStart;
969,577,050✔
1391

1392
  int32_t code1 = handleQueryExecRsp(pRequest);
969,597,534✔
1393
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
969,602,185✔
1394
    pRequest->code = code1;
×
1395
  }
1396

1397
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
1,926,353,739✔
1398
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
956,697,354✔
1399
    continueInsertFromCsv(pWrapper, pRequest);
13,457✔
1400
    return;
13,457✔
1401
  }
1402

1403
  if (pRequest->relation.nextRefId) {
969,643,902✔
1404
    handlePostSubQuery(pWrapper);
×
1405
  } else {
1406
    destorySqlCallbackWrapper(pWrapper);
969,613,948✔
1407
    pRequest->pWrapper = NULL;
969,554,679✔
1408

1409
    // return to client
1410
    doRequestCallback(pRequest, code);
969,573,750✔
1411
  }
1412
}
1413

1414
void launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res) {
9,385,437✔
1415
  int32_t code = 0;
9,385,437✔
1416
  int32_t subplanNum = 0;
9,385,437✔
1417

1418
  if (pQuery->pRoot) {
9,385,437✔
1419
    pRequest->stmtType = pQuery->pRoot->type;
8,930,432✔
1420
    if (nodeType(pQuery->pRoot) == QUERY_NODE_DELETE_STMT) {
8,927,798✔
1421
      pRequest->secureDelete = ((SDeleteStmt*)pQuery->pRoot)->secureDelete;
×
1422
    }
1423
  }
1424

1425
  if (pQuery->pRoot && !pRequest->inRetry) {
9,390,154✔
1426
    STscObj*            pTscObj = pRequest->pTscObj;
8,928,417✔
1427
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
8,931,695✔
1428
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) {
8,933,642✔
1429
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
8,907,851✔
1430
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
22,889✔
1431
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
22,895✔
1432
    }
1433
  }
1434

1435
  pRequest->body.execMode = pQuery->execMode;
9,400,100✔
1436
  switch (pQuery->execMode) {
9,391,816✔
1437
    case QUERY_EXEC_MODE_LOCAL:
×
1438
      if (!pRequest->validateOnly) {
×
1439
        if (NULL == pQuery->pRoot) {
×
1440
          terrno = TSDB_CODE_INVALID_PARA;
×
1441
          code = terrno;
×
1442
        } else {
1443
          code = execLocalCmd(pRequest, pQuery);
×
1444
        }
1445
      }
1446
      break;
×
1447
    case QUERY_EXEC_MODE_RPC:
462,003✔
1448
      if (!pRequest->validateOnly) {
462,003✔
1449
        code = execDdlQuery(pRequest, pQuery);
462,003✔
1450
      }
1451
      break;
462,015✔
1452
    case QUERY_EXEC_MODE_SCHEDULE: {
8,919,284✔
1453
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
8,919,284✔
1454
      if (NULL == pMnodeList) {
8,923,201✔
1455
        code = terrno;
×
1456
        break;
×
1457
      }
1458
      SQueryPlan* pDag = NULL;
8,923,201✔
1459
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
8,923,201✔
1460
      if (TSDB_CODE_SUCCESS == code) {
8,927,985✔
1461
        pRequest->body.subplanNum = pDag->numOfSubplans;
8,929,492✔
1462
        if (!pRequest->validateOnly) {
8,929,461✔
1463
          SArray* pNodeList = NULL;
8,921,118✔
1464
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
8,923,769✔
1465

1466
          if (TSDB_CODE_SUCCESS == code) {
8,930,587✔
1467
            code = sessMetricCheckValue((SSessMetric*)pRequest->pTscObj->pSessMetric, SESSION_MAX_CALL_VNODE_NUM,
8,933,711✔
1468
                                        taosArrayGetSize(pNodeList));
8,932,496✔
1469
          }
1470

1471
          if (TSDB_CODE_SUCCESS == code) {
8,924,039✔
1472
            code = scheduleQuery(pRequest, pDag, pNodeList);
8,924,039✔
1473
          }
1474
          taosArrayDestroy(pNodeList);
8,932,903✔
1475
        }
1476
      }
1477
      taosArrayDestroy(pMnodeList);
8,928,398✔
1478
      break;
8,934,708✔
1479
    }
1480
    case QUERY_EXEC_MODE_EMPTY_RESULT:
×
1481
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
1482
      break;
×
1483
    default:
×
1484
      break;
×
1485
  }
1486

1487
  if (!keepQuery) {
9,398,195✔
1488
    qDestroyQuery(pQuery);
×
1489
  }
1490

1491
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
9,398,195✔
1492
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
30,187✔
1493
    if (TSDB_CODE_SUCCESS != ret) {
30,187✔
1494
      tscError("req:0x%" PRIx64 ", remove meta failed,code:%d, QID:0x%" PRIx64, pRequest->self, ret,
×
1495
               pRequest->requestId);
1496
    }
1497
  }
1498

1499
  if (TSDB_CODE_SUCCESS == code) {
9,396,903✔
1500
    code = handleQueryExecRsp(pRequest);
9,394,314✔
1501
  }
1502

1503
  if (TSDB_CODE_SUCCESS != code) {
9,396,793✔
1504
    pRequest->code = code;
7,718✔
1505
  }
1506

1507
  if (res) {
9,396,793✔
1508
    *res = pRequest->body.resInfo.execRes.res;
×
1509
    pRequest->body.resInfo.execRes.res = NULL;
×
1510
  }
1511
}
9,396,793✔
1512

1513
static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta,
970,023,195✔
1514
                                 SSqlCallbackWrapper* pWrapper) {
1515
  int32_t code = TSDB_CODE_SUCCESS;
970,023,195✔
1516
  pRequest->type = pQuery->msgType;
970,023,195✔
1517
  SArray*     pMnodeList = NULL;
970,063,319✔
1518
  SArray*     pNodeList = NULL;
970,063,319✔
1519
  SQueryPlan* pDag = NULL;
970,034,551✔
1520
  int64_t     st = taosGetTimestampUs();
970,066,552✔
1521

1522
  if (!pRequest->parseOnly) {
970,066,552✔
1523
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_PLAN);
1,940,137,612✔
1524

1525
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
970,253,794✔
1526
    if (NULL == pMnodeList) {
969,990,396✔
1527
      code = terrno;
×
1528
    }
1529
    SPlanContext cxt = {.queryId = pRequest->requestId,
1,055,294,742✔
1530
                        .acctId = pRequest->pTscObj->acctId,
970,105,592✔
1531
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
970,149,275✔
1532
                        .pAstRoot = pQuery->pRoot,
970,180,270✔
1533
                        .showRewrite = pQuery->showRewrite,
970,187,741✔
1534
                        .isView = pWrapper->pParseCtx->isView,
970,176,878✔
1535
                        .isAudit = pWrapper->pParseCtx->isAudit,
970,170,125✔
1536
                        .privInfo = pWrapper->pParseCtx->privInfo,
970,076,325✔
1537
                        .pMsg = pRequest->msgBuf,
970,145,310✔
1538
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1539
                        .pUser = pRequest->pTscObj->user,
970,136,068✔
1540
                        .userId = pRequest->pTscObj->userId,
970,104,124✔
1541
                        .sysInfo = pRequest->pTscObj->sysInfo,
970,043,972✔
1542
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
970,158,612✔
1543
                        .allocatorId = pRequest->stmtBindVersion > 0 ? 0 : pRequest->allocatorRefId};
970,084,696✔
1544
    if (TSDB_CODE_SUCCESS == code) {
970,122,298✔
1545
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
970,064,102✔
1546
    }
1547
    if (code) {
969,964,091✔
1548
      tscError("req:0x%" PRIx64 " requestId:0x%" PRIx64 ", failed to create query plan, code:%s msg:%s", pRequest->self,
275,914✔
1549
               pRequest->requestId, tstrerror(code), cxt.pMsg);
1550
    } else {
1551
      pRequest->body.subplanNum = pDag->numOfSubplans;
969,688,177✔
1552
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
969,846,861✔
1553
    }
1554
  }
1555

1556
  pRequest->metric.execStart = taosGetTimestampUs();
970,029,487✔
1557
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
969,966,109✔
1558

1559
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
970,096,001✔
1560
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
969,495,679✔
1561
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_SCHEDULE);
415,765,508✔
1562
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
207,890,346✔
1563
    }
1564

1565
    if (code == TSDB_CODE_SUCCESS) {
969,614,671✔
1566
      code = sessMetricCheckValue((SSessMetric*)pRequest->pTscObj->pSessMetric, SESSION_MAX_CALL_VNODE_NUM,
969,594,366✔
1567
                                  taosArrayGetSize(pNodeList));
969,589,524✔
1568
    }
1569

1570
    if (code == TSDB_CODE_SUCCESS) {
969,604,910✔
1571
      SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
969,601,452✔
1572
                               .requestId = pRequest->requestId,
969,637,817✔
1573
                               .requestObjRefId = pRequest->self};
969,590,909✔
1574
      SSchedulerReq    req = {
1,012,267,650✔
1575
             .syncReq = false,
1576
             .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
969,590,996✔
1577
             .pConn = &conn,
1578
             .pNodeList = pNodeList,
1579
             .pDag = pDag,
1580
             .allocatorRefId = pRequest->allocatorRefId,
969,590,996✔
1581
             .sql = pRequest->sqlstr,
969,555,523✔
1582
             .startTs = pRequest->metric.start,
969,565,737✔
1583
             .execFp = schedulerExecCb,
1584
             .cbParam = pWrapper,
1585
             .chkKillFp = chkRequestKilled,
1586
             .chkKillParam = (void*)pRequest->self,
969,612,424✔
1587
             .pExecRes = NULL,
1588
             .source = pRequest->source,
969,547,986✔
1589
             .secureDelete = pRequest->secureDelete,
969,600,576✔
1590
             .pWorkerCb = getTaskPoolWorkerCb(),
969,563,687✔
1591
      };
1592

1593
      if (TSDB_CODE_SUCCESS == code) {
969,476,566✔
1594
        CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_EXECUTE);
1,939,276,107✔
1595
        code = schedulerExecJob(&req, &pRequest->body.queryJob);
969,645,512✔
1596
      }
1597
      taosArrayDestroy(pNodeList);
969,459,254✔
1598
      taosArrayDestroy(pMnodeList);
969,640,469✔
1599
      return code;
969,644,198✔
1600
    }
1601
  }
1602

1603
  qDestroyQueryPlan(pDag);
651,124✔
1604
  tscDebug("req:0x%" PRIx64 ", plan not executed, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
485,934✔
1605
           pRequest->requestId);
1606
  destorySqlCallbackWrapper(pWrapper);
485,934✔
1607
  pRequest->pWrapper = NULL;
485,934✔
1608
  if (TSDB_CODE_SUCCESS != code) {
485,934✔
1609
    pRequest->code = code;
279,372✔
1610
  }
1611

1612
  doRequestCallback(pRequest, code);
485,934✔
1613

1614
  // todo not to be released here
1615
  taosArrayDestroy(pMnodeList);
485,934✔
1616
  taosArrayDestroy(pNodeList);
485,934✔
1617

1618
  return code;
483,077✔
1619
}
1620

1621
void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta, SSqlCallbackWrapper* pWrapper) {
1,081,503,028✔
1622
  int32_t code = 0;
1,081,503,028✔
1623

1624
  if (pRequest->parseOnly) {
1,081,503,028✔
1625
    doRequestCallback(pRequest, 0);
322,221✔
1626
    return;
322,221✔
1627
  }
1628

1629
  pRequest->body.execMode = pQuery->execMode;
1,081,273,262✔
1630
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
1,081,270,389✔
1631
    destorySqlCallbackWrapper(pWrapper);
111,193,798✔
1632
    pRequest->pWrapper = NULL;
111,197,532✔
1633
  }
1634

1635
  if (pQuery->pRoot && !pRequest->inRetry) {
1,081,236,356✔
1636
    STscObj*            pTscObj = pRequest->pTscObj;
1,081,229,397✔
1637
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
1,081,331,523✔
1638
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
1,081,302,701✔
1639
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
761,769,098✔
1640
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
693,198,737✔
1641
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
388,142,730✔
1642
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
157,825,968✔
1643
    }
1644
  }
1645

1646
  switch (pQuery->execMode) {
1,081,374,394✔
1647
    case QUERY_EXEC_MODE_LOCAL:
6,659,113✔
1648
      asyncExecLocalCmd(pRequest, pQuery);
6,659,113✔
1649
      break;
6,659,129✔
1650
    case QUERY_EXEC_MODE_RPC:
103,886,357✔
1651
      code = asyncExecDdlQuery(pRequest, pQuery);
103,886,357✔
1652
      break;
103,883,746✔
1653
    case QUERY_EXEC_MODE_SCHEDULE: {
970,109,152✔
1654
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
970,109,152✔
1655
      break;
970,093,374✔
1656
    }
1657
    case QUERY_EXEC_MODE_EMPTY_RESULT:
645,391✔
1658
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
645,391✔
1659
      doRequestCallback(pRequest, 0);
645,391✔
1660
      break;
645,391✔
1661
    default:
×
1662
      tscError("req:0x%" PRIx64 ", invalid execMode %d", pRequest->self, pQuery->execMode);
×
1663
      doRequestCallback(pRequest, -1);
×
1664
      break;
×
1665
  }
1666
}
1667

1668
int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) {
17,913✔
1669
  SCatalog* pCatalog = NULL;
17,913✔
1670
  int32_t   code = 0;
17,913✔
1671
  int32_t   dbNum = taosArrayGetSize(pRequest->dbList);
17,913✔
1672
  int32_t   tblNum = taosArrayGetSize(pRequest->tableList);
17,913✔
1673

1674
  if (dbNum <= 0 && tblNum <= 0) {
17,913✔
1675
    return TSDB_CODE_APP_ERROR;
17,121✔
1676
  }
1677

1678
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
792✔
1679
  if (code != TSDB_CODE_SUCCESS) {
792✔
1680
    return code;
×
1681
  }
1682

1683
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
792✔
1684
                           .requestId = pRequest->requestId,
792✔
1685
                           .requestObjRefId = pRequest->self,
792✔
1686
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
792✔
1687

1688
  for (int32_t i = 0; i < dbNum; ++i) {
1,584✔
1689
    char* dbFName = taosArrayGet(pRequest->dbList, i);
792✔
1690

1691
    // catalogRefreshDBVgInfo will handle dbFName == null.
1692
    code = catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
792✔
1693
    if (code != TSDB_CODE_SUCCESS) {
792✔
1694
      return code;
×
1695
    }
1696
  }
1697

1698
  for (int32_t i = 0; i < tblNum; ++i) {
2,178✔
1699
    SName* tableName = taosArrayGet(pRequest->tableList, i);
1,584✔
1700

1701
    // catalogRefreshTableMeta will handle tableName == null.
1702
    code = catalogRefreshTableMeta(pCatalog, &conn, tableName, -1);
1,584✔
1703
    if (code != TSDB_CODE_SUCCESS) {
1,584✔
1704
      return code;
198✔
1705
    }
1706
  }
1707

1708
  return code;
594✔
1709
}
1710

1711
int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView) {
5,441,926✔
1712
  SCatalog* pCatalog = NULL;
5,441,926✔
1713
  int32_t   tbNum = taosArrayGetSize(tbList);
5,441,926✔
1714
  int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
5,441,926✔
1715
  if (code != TSDB_CODE_SUCCESS) {
5,441,926✔
1716
    return code;
×
1717
  }
1718

1719
  if (isView) {
5,441,926✔
1720
    for (int32_t i = 0; i < tbNum; ++i) {
758,790✔
1721
      SName* pViewName = taosArrayGet(tbList, i);
379,395✔
1722
      char   dbFName[TSDB_DB_FNAME_LEN];
375,861✔
1723
      if (NULL == pViewName) {
379,395✔
1724
        continue;
×
1725
      }
1726
      (void)tNameGetFullDbName(pViewName, dbFName);
379,395✔
1727
      TSC_ERR_RET(catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0));
379,395✔
1728
    }
1729
  } else {
1730
    for (int32_t i = 0; i < tbNum; ++i) {
7,996,686✔
1731
      SName* pTbName = taosArrayGet(tbList, i);
2,934,155✔
1732
      TSC_ERR_RET(catalogRemoveTableMeta(pCatalog, pTbName));
2,934,155✔
1733
    }
1734
  }
1735

1736
  return TSDB_CODE_SUCCESS;
5,441,926✔
1737
}
1738

1739
int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) {
87,748,442✔
1740
  pEpSet->version = 0;
87,748,442✔
1741

1742
  // init mnode ip set
1743
  SEpSet* mgmtEpSet = &(pEpSet->epSet);
87,750,334✔
1744
  mgmtEpSet->numOfEps = 0;
87,749,648✔
1745
  mgmtEpSet->inUse = 0;
87,749,563✔
1746

1747
  if (firstEp && firstEp[0] != 0) {
87,749,500✔
1748
    if (strlen(firstEp) >= TSDB_EP_LEN) {
87,750,869✔
1749
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1750
      return -1;
×
1751
    }
1752

1753
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
87,750,869✔
1754
    if (code != TSDB_CODE_SUCCESS) {
87,749,193✔
1755
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1756
      return terrno;
×
1757
    }
1758
    // uint32_t addr = 0;
1759
    SIpAddr addr = {0};
87,749,193✔
1760
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
87,750,244✔
1761
    if (code) {
87,750,114✔
1762
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
136✔
1763
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1764
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
150✔
1765
    } else {
1766
      mgmtEpSet->numOfEps++;
87,752,592✔
1767
    }
1768
  }
1769

1770
  if (secondEp && secondEp[0] != 0) {
87,749,689✔
1771
    if (strlen(secondEp) >= TSDB_EP_LEN) {
2,080,811✔
1772
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1773
      return terrno;
×
1774
    }
1775

1776
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
2,080,811✔
1777
    if (code != TSDB_CODE_SUCCESS) {
2,080,765✔
1778
      return code;
×
1779
    }
1780
    SIpAddr addr = {0};
2,080,765✔
1781
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
2,080,765✔
1782
    if (code) {
2,080,789✔
1783
      tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
×
1784
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1785
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
×
1786
    } else {
1787
      mgmtEpSet->numOfEps++;
2,080,789✔
1788
    }
1789
  }
1790

1791
  if (mgmtEpSet->numOfEps == 0) {
87,749,683✔
1792
    terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
150✔
1793
    return TSDB_CODE_RPC_NETWORK_UNAVAIL;
150✔
1794
  }
1795

1796
  return 0;
87,750,126✔
1797
}
1798

1799
int32_t taosConnectImpl(const char* user, const char* auth, int32_t totpCode, const char* db, __taos_async_fn_t fp,
87,750,833✔
1800
                        void* param, SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj) {
1801
  *pTscObj = NULL;
87,750,833✔
1802
  int32_t code = createTscObj(user, auth, db, connType, pAppInfo, pTscObj);
87,750,833✔
1803
  if (TSDB_CODE_SUCCESS != code) {
87,753,060✔
1804
    return code;
×
1805
  }
1806

1807
  SRequestObj* pRequest = NULL;
87,753,060✔
1808
  code = createRequest((*pTscObj)->id, TDMT_MND_CONNECT, 0, &pRequest);
87,753,060✔
1809
  if (TSDB_CODE_SUCCESS != code) {
87,753,230✔
1810
    destroyTscObj(*pTscObj);
×
1811
    return code;
×
1812
  }
1813

1814
  pRequest->sqlstr = taosStrdup("taos_connect");
87,753,230✔
1815
  if (pRequest->sqlstr) {
87,752,753✔
1816
    pRequest->sqlLen = strlen(pRequest->sqlstr);
87,752,749✔
1817
  } else {
1818
    return terrno;
×
1819
  }
1820

1821
  SMsgSendInfo* body = NULL;
87,752,753✔
1822
  code = buildConnectMsg(pRequest, &body, totpCode);
87,752,753✔
1823
  if (TSDB_CODE_SUCCESS != code) {
87,749,614✔
1824
    destroyTscObj(*pTscObj);
×
1825
    return code;
×
1826
  }
1827

1828
  // int64_t transporterId = 0;
1829
  SEpSet epset = getEpSet_s(&(*pTscObj)->pAppInfo->mgmtEp);
87,749,614✔
1830
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &epset, NULL, body);
87,751,517✔
1831
  if (TSDB_CODE_SUCCESS != code) {
87,749,860✔
1832
    destroyTscObj(*pTscObj);
×
1833
    tscError("failed to send connect msg to server, code:%s", tstrerror(code));
×
1834
    return code;
×
1835
  }
1836
  if (TSDB_CODE_SUCCESS != tsem_wait(&pRequest->body.rspSem)) {
87,749,860✔
1837
    destroyTscObj(*pTscObj);
5✔
1838
    tscError("failed to wait sem, code:%s", terrstr());
×
1839
    return terrno;
×
1840
  }
1841
  if (pRequest->code != TSDB_CODE_SUCCESS) {
87,751,484✔
1842
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
18,862✔
1843
    tscError("failed to connect to server, reason: %s", errorMsg);
18,862✔
1844

1845
    terrno = pRequest->code;
18,862✔
1846
    destroyRequest(pRequest);
18,862✔
1847
    taos_close_internal(*pTscObj);
18,862✔
1848
    *pTscObj = NULL;
18,862✔
1849
    return terrno;
18,862✔
1850
  }
1851
  if (connType == CONN_TYPE__AUTH_TEST) {
87,732,622✔
1852
    terrno = TSDB_CODE_SUCCESS;
98✔
1853
    destroyRequest(pRequest);
98✔
1854
    taos_close_internal(*pTscObj);
98✔
1855
    *pTscObj = NULL;
1,522✔
1856
    return TSDB_CODE_SUCCESS;
1,522✔
1857
  }
1858

1859
  tscInfo("conn:0x%" PRIx64 ", connection is opening, connId:%u, dnodeConn:%p, QID:0x%" PRIx64, (*pTscObj)->id,
87,732,524✔
1860
          (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId);
1861
  destroyRequest(pRequest);
87,734,451✔
1862
  return code;
87,729,993✔
1863
}
1864

1865
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo, int32_t totpCode) {
87,750,327✔
1866
  *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
87,750,327✔
1867
  if (*pMsgSendInfo == NULL) {
87,752,590✔
1868
    return terrno;
×
1869
  }
1870

1871
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
87,752,590✔
1872

1873
  (*pMsgSendInfo)->requestObjRefId = pRequest->self;
87,752,590✔
1874
  (*pMsgSendInfo)->requestId = pRequest->requestId;
87,752,590✔
1875
  (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType);
87,752,590✔
1876
  (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self));
87,752,131✔
1877
  if (NULL == (*pMsgSendInfo)->param) {
87,752,869✔
1878
    taosMemoryFree(*pMsgSendInfo);
×
1879
    return terrno;
×
1880
  }
1881

1882
  *(int64_t*)(*pMsgSendInfo)->param = pRequest->self;
87,752,869✔
1883

1884
  SConnectReq connectReq = {0};
87,752,869✔
1885
  STscObj*    pObj = pRequest->pTscObj;
87,752,869✔
1886

1887
  char* db = getDbOfConnection(pObj);
87,752,853✔
1888
  if (db != NULL) {
87,752,135✔
1889
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
481,333✔
1890
  } else if (terrno) {
87,270,802✔
1891
    taosMemoryFree(*pMsgSendInfo);
×
1892
    return terrno;
×
1893
  }
1894
  taosMemoryFreeClear(db);
87,753,252✔
1895

1896
  connectReq.connType = pObj->connType;
87,753,264✔
1897
  connectReq.pid = appInfo.pid;
87,753,264✔
1898
  connectReq.startTime = appInfo.startTime;
87,753,264✔
1899
  connectReq.totpCode = totpCode;
87,753,264✔
1900
  connectReq.connectTime = taosGetTimestampMs();
87,750,497✔
1901

1902
  tstrncpy(connectReq.app, appInfo.appName, sizeof(connectReq.app));
87,750,497✔
1903
  tstrncpy(connectReq.user, pObj->user, sizeof(connectReq.user));
87,750,497✔
1904
  tstrncpy(connectReq.passwd, pObj->pass, sizeof(connectReq.passwd));
87,750,497✔
1905
  tstrncpy(connectReq.token, pObj->token, sizeof(connectReq.token));
87,750,497✔
1906
  tstrncpy(connectReq.sVer, td_version, sizeof(connectReq.sVer));
87,750,497✔
1907
  tSignConnectReq(&connectReq);
87,750,497✔
1908

1909
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
87,752,637✔
1910
  void*   pReq = taosMemoryMalloc(contLen);
87,750,046✔
1911
  if (NULL == pReq) {
87,752,273✔
1912
    taosMemoryFree(*pMsgSendInfo);
×
1913
    return terrno;
×
1914
  }
1915

1916
  if (-1 == tSerializeSConnectReq(pReq, contLen, &connectReq)) {
87,752,273✔
1917
    taosMemoryFree(*pMsgSendInfo);
×
1918
    taosMemoryFree(pReq);
×
1919
    return terrno;
×
1920
  }
1921

1922
  (*pMsgSendInfo)->msgInfo.len = contLen;
87,750,427✔
1923
  (*pMsgSendInfo)->msgInfo.pData = pReq;
87,750,431✔
1924
  return TSDB_CODE_SUCCESS;
87,749,210✔
1925
}
1926

1927
void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, SEpSet* pEpSet) {
2,147,483,647✔
1928
  if (NULL == pEpSet) {
2,147,483,647✔
1929
    return;
2,147,483,647✔
1930
  }
1931

1932
  switch (pSendInfo->target.type) {
4,654,730✔
1933
    case TARGET_TYPE_MNODE:
326✔
1934
      if (NULL == pTscObj) {
326✔
1935
        tscError("mnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1936
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1937
        return;
792✔
1938
      }
1939

1940
      SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
326✔
1941
      SEpSet* pOrig = &originEpset;
326✔
1942
      SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
326✔
1943
      SEp*    pNewEp = &pEpSet->eps[pEpSet->inUse];
326✔
1944
      tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in client", pOrig->inUse, pOrig->numOfEps,
326✔
1945
               pOrigEp->fqdn, pOrigEp->port, pEpSet->inUse, pEpSet->numOfEps, pNewEp->fqdn, pNewEp->port);
1946
      updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
326✔
1947
      break;
553,996✔
1948
    case TARGET_TYPE_VNODE: {
4,411,004✔
1949
      if (NULL == pTscObj) {
4,411,004✔
1950
        tscError("vnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
792✔
1951
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1952
        return;
792✔
1953
      }
1954

1955
      SCatalog* pCatalog = NULL;
4,410,212✔
1956
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
4,410,212✔
1957
      if (code != TSDB_CODE_SUCCESS) {
4,410,137✔
1958
        tscError("fail to get catalog handle, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1959
                 tstrerror(code));
1960
        return;
×
1961
      }
1962

1963
      code = catalogUpdateVgEpSet(pCatalog, pSendInfo->target.dbFName, pSendInfo->target.vgId, pEpSet);
4,410,137✔
1964
      if (code != TSDB_CODE_SUCCESS) {
4,410,291✔
1965
        tscError("fail to update catalog vg epset, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
112✔
1966
                 tstrerror(code));
1967
        return;
×
1968
      }
1969
      taosMemoryFreeClear(pSendInfo->target.dbFName);
4,410,179✔
1970
      break;
4,410,241✔
1971
    }
1972
    default:
251,080✔
1973
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
251,080✔
1974
      break;
251,780✔
1975
  }
1976
}
1977

1978
int32_t doProcessMsgFromServerImpl(SRpcMsg* pMsg, SEpSet* pEpSet) {
2,147,483,647✔
1979
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
2,147,483,647✔
1980
  if (pMsg->info.ahandle == NULL) {
2,147,483,647✔
1981
    tscError("doProcessMsgFromServer pMsg->info.ahandle == NULL");
659,758✔
1982
    rpcFreeCont(pMsg->pCont);
659,758✔
1983
    taosMemoryFree(pEpSet);
659,758✔
1984
    return TSDB_CODE_TSC_INTERNAL_ERROR;
659,758✔
1985
  }
1986

1987
  STscObj* pTscObj = NULL;
2,147,483,647✔
1988

1989
  STraceId* trace = &pMsg->info.traceId;
2,147,483,647✔
1990
  char      tbuf[40] = {0};
2,147,483,647✔
1991
  TRACE_TO_STR(trace, tbuf);
2,147,483,647✔
1992

1993
  tscDebug("QID:%s, process message from server, handle:%p, message:%s, size:%d, code:%s", tbuf, pMsg->info.handle,
2,147,483,647✔
1994
           TMSG_INFO(pMsg->msgType), pMsg->contLen, tstrerror(pMsg->code));
1995

1996
  if (pSendInfo->requestObjRefId != 0) {
2,147,483,647✔
1997
    SRequestObj* pRequest = (SRequestObj*)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
1,957,425,126✔
1998
    if (pRequest) {
1,957,412,166✔
1999
      if (pRequest->self != pSendInfo->requestObjRefId) {
1,942,801,735✔
2000
        tscError("doProcessMsgFromServer req:0x%" PRId64 " != pSendInfo->requestObjRefId:0x%" PRId64, pRequest->self,
×
2001
                 pSendInfo->requestObjRefId);
2002

2003
        if (TSDB_CODE_SUCCESS != taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId)) {
×
2004
          tscError("doProcessMsgFromServer taosReleaseRef failed");
×
2005
        }
2006
        rpcFreeCont(pMsg->pCont);
×
2007
        taosMemoryFree(pEpSet);
×
2008
        destroySendMsgInfo(pSendInfo);
×
2009
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2010
      }
2011
      pTscObj = pRequest->pTscObj;
1,942,801,221✔
2012
    }
2013
  }
2014

2015
  updateTargetEpSet(pSendInfo, pTscObj, pMsg, pEpSet);
2,147,483,647✔
2016

2017
  SDataBuf buf = {.msgType = pMsg->msgType,
2,147,483,647✔
2018
                  .len = pMsg->contLen,
2,147,483,647✔
2019
                  .pData = NULL,
2020
                  .handle = pMsg->info.handle,
2,147,483,647✔
2021
                  .handleRefId = pMsg->info.refId,
2,147,483,647✔
2022
                  .pEpSet = pEpSet};
2023

2024
  if (pMsg->contLen > 0) {
2,147,483,647✔
2025
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
2,147,483,647✔
2026
    if (buf.pData == NULL) {
2,147,483,647✔
2027
      pMsg->code = terrno;
×
2028
    } else {
2029
      (void)memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
2,147,483,647✔
2030
    }
2031
  }
2032

2033
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
2,147,483,647✔
2034

2035
  if (pTscObj) {
2,147,483,647✔
2036
    int32_t code = taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
1,942,734,087✔
2037
    if (TSDB_CODE_SUCCESS != code) {
1,942,821,491✔
2038
      tscError("doProcessMsgFromServer taosReleaseRef failed");
250✔
2039
      terrno = code;
250✔
2040
      pMsg->code = code;
250✔
2041
    }
2042
  }
2043

2044
  rpcFreeCont(pMsg->pCont);
2,147,483,647✔
2045
  destroySendMsgInfo(pSendInfo);
2,147,483,647✔
2046
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
2047
}
2048

2049
int32_t doProcessMsgFromServer(void* param) {
2,147,483,647✔
2050
  AsyncArg* arg = (AsyncArg*)param;
2,147,483,647✔
2051
  int32_t   code = doProcessMsgFromServerImpl(&arg->msg, arg->pEpset);
2,147,483,647✔
2052
  taosMemoryFree(arg);
2,147,483,647✔
2053
  return code;
2,147,483,647✔
2054
}
2055

2056
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
2,147,483,647✔
2057
  int32_t code = 0;
2,147,483,647✔
2058
  SEpSet* tEpSet = NULL;
2,147,483,647✔
2059

2060
  tscDebug("msg callback, ahandle %p", pMsg->info.ahandle);
2,147,483,647✔
2061

2062
  if (pEpSet != NULL) {
2,147,483,647✔
2063
    tEpSet = taosMemoryCalloc(1, sizeof(SEpSet));
4,663,081✔
2064
    if (NULL == tEpSet) {
4,663,080✔
2065
      code = terrno;
×
2066
      pMsg->code = terrno;
×
2067
      goto _exit;
×
2068
    }
2069
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
4,663,080✔
2070
  }
2071

2072
  // pMsg is response msg
2073
  if (pMsg->msgType == TDMT_MND_CONNECT + 1) {
2,147,483,647✔
2074
    // restore origin code
2075
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
87,688,585✔
2076
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
2077
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
87,688,468✔
2078
      pMsg->code = TSDB_CODE_RPC_BROKEN_LINK;
×
2079
    }
2080
  } else {
2081
    // uniform to one error code: TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED
2082
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
2,147,483,647✔
2083
      pMsg->code = TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED;
×
2084
    }
2085
  }
2086

2087
  AsyncArg* arg = taosMemoryCalloc(1, sizeof(AsyncArg));
2,147,483,647✔
2088
  if (NULL == arg) {
2,147,483,647✔
2089
    code = terrno;
×
2090
    pMsg->code = code;
×
2091
    goto _exit;
×
2092
  }
2093

2094
  arg->msg = *pMsg;
2,147,483,647✔
2095
  arg->pEpset = tEpSet;
2,147,483,647✔
2096

2097
  if ((code = taosAsyncExec(doProcessMsgFromServer, arg, NULL)) != 0) {
2,147,483,647✔
2098
    pMsg->code = code;
408,161✔
2099
    taosMemoryFree(arg);
408,161✔
2100
    goto _exit;
344,280✔
2101
  }
2102
  return;
2,147,483,647✔
2103

2104
_exit:
344,280✔
2105
  tscError("failed to sched msg to tsc since %s", tstrerror(code));
344,280✔
2106
  code = doProcessMsgFromServerImpl(pMsg, tEpSet);
344,280✔
2107
  if (code != 0) {
344,280✔
2108
    tscError("failed to sched msg to tsc, tsc ready quit");
637✔
2109
  }
2110
}
2111

2112
TAOS* taos_connect_totp(const char* ip, const char* user, const char* pass, const char* totp, const char* db,
2,575✔
2113
                        uint16_t port) {
2114
  tscInfo("try to connect to %s:%u by totp, user:%s db:%s", ip, port, user, db);
2,575✔
2115
  if (user == NULL) {
2,575✔
2116
    user = TSDB_DEFAULT_USER;
×
2117
  }
2118

2119
  if (pass == NULL) {
2,575✔
2120
    pass = TSDB_DEFAULT_PASS;
×
2121
  }
2122

2123
  STscObj* pObj = NULL;
2,575✔
2124
  int32_t  code = taos_connect_internal(ip, user, pass, totp, db, port, CONN_TYPE__QUERY, &pObj);
2,575✔
2125
  if (TSDB_CODE_SUCCESS == code) {
2,575✔
2126
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1,710✔
2127
    if (NULL == rid) {
1,710✔
2128
      tscError("out of memory when taos_connect_totp to %s:%u, user:%s db:%s", ip, port, user, db);
×
2129
      return NULL;
×
2130
    }
2131
    *rid = pObj->id;
1,710✔
2132
    return (TAOS*)rid;
1,710✔
2133
  } else {
2134
    terrno = code;
865✔
2135
  }
2136

2137
  return NULL;
865✔
2138
}
2139

2140
int taos_connect_test(const char* ip, const char* user, const char* pass, const char* totp, const char* db,
196✔
2141
                      uint16_t port) {
2142
  tscInfo("try to test connect to %s:%u by totp, user:%s db:%s", ip, port, user, db);
196✔
2143
  if (user == NULL) {
196✔
2144
    user = TSDB_DEFAULT_USER;
×
2145
  }
2146

2147
  if (pass == NULL) {
196✔
2148
    pass = TSDB_DEFAULT_PASS;
×
2149
  }
2150

2151
  STscObj* pObj = NULL;
196✔
2152
  return taos_connect_internal(ip, user, pass, totp, db, port, CONN_TYPE__AUTH_TEST, &pObj);
196✔
2153
}
2154

2155
TAOS* taos_connect_token(const char* ip, const char* token, const char* db, uint16_t port) {
2,907✔
2156
  tscInfo("try to connect to %s:%u by token, db:%s", ip, port, db);
2,907✔
2157

2158
  STscObj* pObj = NULL;
2,907✔
2159
  int32_t  code = taos_connect_by_auth(ip, NULL, token, NULL, db, port, CONN_TYPE__QUERY, &pObj);
2,907✔
2160
  if (TSDB_CODE_SUCCESS == code) {
2,907✔
2161
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1,407✔
2162
    if (NULL == rid) {
1,407✔
2163
      tscError("out of memory when taos_connect_token to %s:%u db:%s", ip, port, db);
×
2164
      return NULL;
×
2165
    }
2166
    *rid = pObj->id;
1,407✔
2167
    return (TAOS*)rid;
1,407✔
2168
  } else {
2169
    terrno = code;
1,500✔
2170
  }
2171

2172
  return NULL;
1,500✔
2173
}
2174

2175
TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, const char* db, uint16_t port) {
136✔
2176
  tscInfo("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
136✔
2177
  if (user == NULL) {
136✔
2178
    user = TSDB_DEFAULT_USER;
×
2179
  }
2180

2181
  if (auth == NULL) {
136✔
2182
    tscError("No auth info is given, failed to connect to server");
×
2183
    return NULL;
×
2184
  }
2185

2186
  STscObj* pObj = NULL;
136✔
2187
  int32_t  code = taos_connect_by_auth(ip, user, auth, NULL, db, port, CONN_TYPE__QUERY, &pObj);
136✔
2188
  if (TSDB_CODE_SUCCESS == code) {
136✔
2189
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
×
2190
    if (NULL == rid) {
×
2191
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
2192
    }
2193
    *rid = pObj->id;
×
2194
    return (TAOS*)rid;
×
2195
  }
2196

2197
  return NULL;
136✔
2198
}
2199

2200
void doSetOneRowPtr(SReqResultInfo* pResultInfo) {
2,147,483,647✔
2201
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,147,483,647✔
2202
    SResultColumn* pCol = &pResultInfo->pCol[i];
2,147,483,647✔
2203

2204
    int32_t type = pResultInfo->fields[i].type;
2,147,483,647✔
2205
    int32_t schemaBytes = calcSchemaBytesFromTypeBytes(type, pResultInfo->userFields[i].bytes, false);
2,147,483,647✔
2206

2207
    if (IS_VAR_DATA_TYPE(type)) {
2,147,483,647✔
2208
      if (!IS_VAR_NULL_TYPE(type, schemaBytes) && pCol->offset[pResultInfo->current] != -1) {
2,147,483,647✔
2209
        char* pStart = pResultInfo->pCol[i].offset[pResultInfo->current] + pResultInfo->pCol[i].pData;
2,147,483,647✔
2210

2211
        if (IS_STR_DATA_BLOB(type)) {
2,147,483,647✔
2212
          pResultInfo->length[i] = blobDataLen(pStart);
17,608✔
2213
          pResultInfo->row[i] = blobDataVal(pStart);
396✔
2214
        } else {
2215
          pResultInfo->length[i] = varDataLen(pStart);
2,147,483,647✔
2216
          pResultInfo->row[i] = varDataVal(pStart);
2,147,483,647✔
2217
        }
2218
      } else {
2219
        pResultInfo->row[i] = NULL;
295,941,478✔
2220
        pResultInfo->length[i] = 0;
296,024,680✔
2221
      }
2222
    } else {
2223
      if (!colDataIsNull_f(pCol, pResultInfo->current)) {
2,147,483,647✔
2224
        pResultInfo->row[i] = pResultInfo->pCol[i].pData + schemaBytes * pResultInfo->current;
2,147,483,647✔
2225
        pResultInfo->length[i] = schemaBytes;
2,147,483,647✔
2226
      } else {
2227
        pResultInfo->row[i] = NULL;
1,270,028,091✔
2228
        pResultInfo->length[i] = 0;
1,270,308,500✔
2229
      }
2230
    }
2231
  }
2232
}
2,147,483,647✔
2233

2234
void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
×
2235
  if (pRequest == NULL) {
×
2236
    return NULL;
×
2237
  }
2238

2239
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
×
2240
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
×
2241
    // All data has returned to App already, no need to try again
2242
    if (pResultInfo->completed) {
×
2243
      pResultInfo->numOfRows = 0;
×
2244
      return NULL;
×
2245
    }
2246

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

2250
    pRequest->code = schedulerFetchRows(pRequest->body.queryJob, &req);
×
2251
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2252
      pResultInfo->numOfRows = 0;
×
2253
      return NULL;
×
2254
    }
2255

2256
    pRequest->code = setQueryResultFromRsp(&pRequest->body.resInfo, (const SRetrieveTableRsp*)pResInfo->pData,
×
2257
                                           convertUcs4, pRequest->stmtBindVersion > 0);
×
2258
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2259
      pResultInfo->numOfRows = 0;
×
2260
      return NULL;
×
2261
    }
2262

2263
    tscDebug("req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64
×
2264
             ", complete:%d, QID:0x%" PRIx64,
2265
             pRequest->self, pResInfo->numOfRows, pResInfo->totalRows, pResInfo->completed, pRequest->requestId);
2266

2267
    STscObj*            pTscObj = pRequest->pTscObj;
×
2268
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
×
2269
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
×
2270

2271
    if (pResultInfo->numOfRows == 0) {
×
2272
      return NULL;
×
2273
    }
2274
  }
2275

2276
  if (setupOneRowPtr) {
×
2277
    doSetOneRowPtr(pResultInfo);
×
2278
    pResultInfo->current += 1;
×
2279
  }
2280

2281
  return pResultInfo->row;
×
2282
}
2283

2284
static void syncFetchFn(void* param, TAOS_RES* res, int32_t numOfRows) {
228,525,545✔
2285
  tsem_t* sem = param;
228,525,545✔
2286
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
228,525,545✔
2287
    tscError("failed to post sem, code:%s", terrstr());
×
2288
  }
2289
}
228,525,584✔
2290

2291
void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
1,783,435,395✔
2292
  if (pRequest == NULL) {
1,783,435,395✔
2293
    return NULL;
×
2294
  }
2295

2296
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
1,783,435,395✔
2297
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
1,783,435,554✔
2298
    // All data has returned to App already, no need to try again
2299
    if (pResultInfo->completed) {
330,500,820✔
2300
      pResultInfo->numOfRows = 0;
101,975,240✔
2301
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
203,950,619✔
2302
      return NULL;
101,975,315✔
2303
    }
2304

2305
    // convert ucs4 to native multi-bytes string
2306
    pResultInfo->convertUcs4 = convertUcs4;
228,525,558✔
2307
    tsem_t sem;
227,851,151✔
2308
    if (TSDB_CODE_SUCCESS != tsem_init(&sem, 0, 0)) {
228,525,515✔
2309
      tscError("failed to init sem, code:%s", terrstr());
×
2310
    }
2311
    taos_fetch_rows_a(pRequest, syncFetchFn, &sem);
228,525,360✔
2312
    if (TSDB_CODE_SUCCESS != tsem_wait(&sem)) {
228,525,515✔
2313
      tscError("failed to wait sem, code:%s", terrstr());
×
2314
    }
2315
    if (TSDB_CODE_SUCCESS != tsem_destroy(&sem)) {
228,525,584✔
2316
      tscError("failed to destroy sem, code:%s", terrstr());
×
2317
    }
2318
    pRequest->inCallback = false;
228,525,584✔
2319
  }
2320

2321
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
1,681,460,284✔
2322
    return NULL;
11,740,621✔
2323
  } else {
2324
    if (setupOneRowPtr) {
1,669,719,802✔
2325
      doSetOneRowPtr(pResultInfo);
1,451,433,260✔
2326
      pResultInfo->current += 1;
1,451,433,059✔
2327
    }
2328

2329
    return pResultInfo->row;
1,669,719,571✔
2330
  }
2331
}
2332

2333
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
338,858,961✔
2334
  if (pResInfo->row == NULL) {
338,858,961✔
2335
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
219,798,106✔
2336
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
219,797,199✔
2337
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
219,790,444✔
2338
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
219,796,644✔
2339

2340
    if (pResInfo->row == NULL || pResInfo->pCol == NULL || pResInfo->length == NULL || pResInfo->convertBuf == NULL) {
219,796,420✔
2341
      taosMemoryFree(pResInfo->row);
783✔
2342
      taosMemoryFree(pResInfo->pCol);
×
2343
      taosMemoryFree(pResInfo->length);
×
2344
      taosMemoryFree(pResInfo->convertBuf);
×
2345
      return terrno;
×
2346
    }
2347
  }
2348

2349
  return TSDB_CODE_SUCCESS;
338,858,569✔
2350
}
2351

2352
static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength, bool isStmt) {
338,689,373✔
2353
  int32_t idx = -1;
338,689,373✔
2354
  iconv_t conv = taosAcquireConv(&idx, C2M, pResultInfo->charsetCxt);
338,690,761✔
2355
  if (conv == (iconv_t)-1) return TSDB_CODE_TSC_INTERNAL_ERROR;
338,688,331✔
2356

2357
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
1,973,340,270✔
2358
    int32_t type = pResultInfo->fields[i].type;
1,634,663,257✔
2359
    int32_t schemaBytes =
2360
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
1,634,668,618✔
2361

2362
    if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) {
1,634,658,656✔
2363
      char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]);
100,262,257✔
2364
      if (p == NULL) {
100,262,257✔
2365
        taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2366
        return terrno;
×
2367
      }
2368

2369
      pResultInfo->convertBuf[i] = p;
100,262,257✔
2370

2371
      SResultColumn* pCol = &pResultInfo->pCol[i];
100,262,257✔
2372
      for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
2,147,483,647✔
2373
        if (pCol->offset[j] != -1) {
2,147,483,647✔
2374
          char* pStart = pCol->offset[j] + pCol->pData;
2,147,483,647✔
2375

2376
          int32_t len = taosUcs4ToMbsEx((TdUcs4*)varDataVal(pStart), varDataLen(pStart), varDataVal(p), conv);
2,147,483,647✔
2377
          if (len < 0 || len > schemaBytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) {
2,147,483,647✔
2378
            tscError(
66✔
2379
                "doConvertUCS4 error, invalid data. len:%d, bytes:%d, (p + len):%p, (pResultInfo->convertBuf[i] + "
2380
                "colLength[i]):%p",
2381
                len, schemaBytes, (p + len), (pResultInfo->convertBuf[i] + colLength[i]));
2382
            taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
66✔
2383
            return TSDB_CODE_TSC_INTERNAL_ERROR;
66✔
2384
          }
2385

2386
          varDataSetLen(p, len);
2,147,483,647✔
2387
          pCol->offset[j] = (p - pResultInfo->convertBuf[i]);
2,147,483,647✔
2388
          p += (len + VARSTR_HEADER_SIZE);
2,147,483,647✔
2389
        }
2390
      }
2391

2392
      pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
100,262,191✔
2393
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
100,262,191✔
2394
    }
2395
  }
2396
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
338,688,683✔
2397
  return TSDB_CODE_SUCCESS;
338,690,255✔
2398
}
2399

2400
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
338,685,200✔
2401
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
1,973,343,008✔
2402
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
1,634,669,518✔
2403
    TAOS_FIELD*   pField = pResultInfo->userFields + i;
1,634,669,928✔
2404
    int32_t       type = pFieldE->type;
1,634,667,189✔
2405
    int32_t       bufLen = 0;
1,634,670,402✔
2406
    char*         p = NULL;
1,634,670,402✔
2407
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
1,634,670,402✔
2408
      continue;
1,632,914,500✔
2409
    } else {
2410
      bufLen = 64;
1,744,843✔
2411
      p = taosMemoryRealloc(pResultInfo->convertBuf[i], bufLen * pResultInfo->numOfRows);
1,744,843✔
2412
      pFieldE->bytes = bufLen;
1,744,843✔
2413
      pField->bytes = bufLen;
1,744,843✔
2414
    }
2415
    if (!p) return terrno;
1,744,843✔
2416
    pResultInfo->convertBuf[i] = p;
1,744,843✔
2417

2418
    for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
1,068,761,398✔
2419
      int32_t code = decimalToStr((DecimalWord*)(pResultInfo->pCol[i].pData + j * tDataTypes[type].bytes), type,
1,067,016,555✔
2420
                                  pFieldE->precision, pFieldE->scale, p, bufLen);
1,067,016,555✔
2421
      p += bufLen;
1,067,016,555✔
2422
      if (TSDB_CODE_SUCCESS != code) {
1,067,016,555✔
2423
        return code;
×
2424
      }
2425
    }
2426
    pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
1,744,843✔
2427
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
1,744,843✔
2428
  }
2429
  return 0;
338,686,671✔
2430
}
2431

2432
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols) {
443,368✔
2433
  return sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) * 3 + sizeof(uint64_t) +
886,146✔
2434
         numOfCols * (sizeof(int8_t) + sizeof(int32_t));
442,778✔
2435
}
2436

2437
static int32_t estimateJsonLen(SReqResultInfo* pResultInfo) {
221,684✔
2438
  char*   p = (char*)pResultInfo->pData;
221,684✔
2439
  int32_t blockVersion = *(int32_t*)p;
221,684✔
2440

2441
  int32_t numOfRows = pResultInfo->numOfRows;
221,684✔
2442
  int32_t numOfCols = pResultInfo->numOfCols;
221,684✔
2443

2444
  // | version | total length | total rows | total columns | flag seg| block group id | column schema | each column
2445
  // length |
2446
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
221,684✔
2447
  if (numOfCols != cols) {
221,684✔
2448
    tscError("estimateJsonLen error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2449
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2450
  }
2451

2452
  int32_t  len = getVersion1BlockMetaSize(p, numOfCols);
221,684✔
2453
  int32_t* colLength = (int32_t*)(p + len);
221,684✔
2454
  len += sizeof(int32_t) * numOfCols;
221,684✔
2455

2456
  char* pStart = p + len;
221,684✔
2457
  for (int32_t i = 0; i < numOfCols; ++i) {
944,508✔
2458
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
722,824✔
2459

2460
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
722,824✔
2461
      int32_t* offset = (int32_t*)pStart;
256,755✔
2462
      int32_t  lenTmp = numOfRows * sizeof(int32_t);
256,755✔
2463
      len += lenTmp;
256,755✔
2464
      pStart += lenTmp;
256,755✔
2465

2466
      int32_t estimateColLen = 0;
256,755✔
2467
      for (int32_t j = 0; j < numOfRows; ++j) {
1,230,641✔
2468
        if (offset[j] == -1) {
973,886✔
2469
          continue;
54,350✔
2470
        }
2471
        char* data = offset[j] + pStart;
919,536✔
2472

2473
        int32_t jsonInnerType = *data;
919,536✔
2474
        char*   jsonInnerData = data + CHAR_BYTES;
919,536✔
2475
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
919,536✔
2476
          estimateColLen += (VARSTR_HEADER_SIZE + strlen(TSDB_DATA_NULL_STR_L));
12,744✔
2477
        } else if (tTagIsJson(data)) {
906,792✔
2478
          estimateColLen += (VARSTR_HEADER_SIZE + ((const STag*)(data))->len);
225,975✔
2479
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
680,817✔
2480
          estimateColLen += varDataTLen(jsonInnerData) + CHAR_BYTES * 2;
633,027✔
2481
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
47,790✔
2482
          estimateColLen += (VARSTR_HEADER_SIZE + 32);
35,046✔
2483
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
12,744✔
2484
          estimateColLen += (VARSTR_HEADER_SIZE + 5);
12,744✔
2485
        } else if (IS_STR_DATA_BLOB(jsonInnerType)) {
×
2486
          estimateColLen += (BLOBSTR_HEADER_SIZE + 32);
×
2487
        } else {
2488
          tscError("estimateJsonLen error: invalid type:%d", jsonInnerType);
×
2489
          return -1;
×
2490
        }
2491
      }
2492
      len += TMAX(colLen, estimateColLen);
256,755✔
2493
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
466,069✔
2494
      int32_t lenTmp = numOfRows * sizeof(int32_t);
63,760✔
2495
      len += (lenTmp + colLen);
63,760✔
2496
      pStart += lenTmp;
63,760✔
2497
    } else {
2498
      int32_t lenTmp = BitmapLen(pResultInfo->numOfRows);
402,309✔
2499
      len += (lenTmp + colLen);
402,309✔
2500
      pStart += lenTmp;
402,309✔
2501
    }
2502
    pStart += colLen;
722,824✔
2503
  }
2504

2505
  // Ensure the complete structure of the block, including the blankfill field,
2506
  // even though it is not used on the client side.
2507
  len += sizeof(bool);
221,684✔
2508
  return len;
221,684✔
2509
}
2510

2511
static int32_t doConvertJson(SReqResultInfo* pResultInfo) {
338,854,776✔
2512
  int32_t numOfRows = pResultInfo->numOfRows;
338,854,776✔
2513
  int32_t numOfCols = pResultInfo->numOfCols;
338,858,868✔
2514
  bool    needConvert = false;
338,859,883✔
2515
  for (int32_t i = 0; i < numOfCols; ++i) {
1,973,935,898✔
2516
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
1,635,296,915✔
2517
      needConvert = true;
221,684✔
2518
      break;
221,684✔
2519
    }
2520
  }
2521

2522
  if (!needConvert) {
338,860,667✔
2523
    return TSDB_CODE_SUCCESS;
338,638,983✔
2524
  }
2525

2526
  tscDebug("start to convert form json format string");
221,684✔
2527

2528
  char*   p = (char*)pResultInfo->pData;
221,684✔
2529
  int32_t blockVersion = *(int32_t*)p;
221,684✔
2530
  int32_t dataLen = estimateJsonLen(pResultInfo);
221,684✔
2531
  if (dataLen <= 0) {
221,684✔
2532
    tscError("doConvertJson error: estimateJsonLen failed");
×
2533
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2534
  }
2535

2536
  taosMemoryFreeClear(pResultInfo->convertJson);
221,684✔
2537
  pResultInfo->convertJson = taosMemoryCalloc(1, dataLen);
221,684✔
2538
  if (pResultInfo->convertJson == NULL) return terrno;
221,684✔
2539
  char* p1 = pResultInfo->convertJson;
221,684✔
2540

2541
  int32_t totalLen = 0;
221,684✔
2542
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
221,684✔
2543
  if (numOfCols != cols) {
221,684✔
2544
    tscError("doConvertJson error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2545
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2546
  }
2547

2548
  int32_t len = getVersion1BlockMetaSize(p, numOfCols);
221,684✔
2549
  (void)memcpy(p1, p, len);
221,684✔
2550

2551
  p += len;
221,684✔
2552
  p1 += len;
221,684✔
2553
  totalLen += len;
221,684✔
2554

2555
  len = sizeof(int32_t) * numOfCols;
221,684✔
2556
  int32_t* colLength = (int32_t*)p;
221,684✔
2557
  int32_t* colLength1 = (int32_t*)p1;
221,684✔
2558
  (void)memcpy(p1, p, len);
221,684✔
2559
  p += len;
221,684✔
2560
  p1 += len;
221,684✔
2561
  totalLen += len;
221,684✔
2562

2563
  char* pStart = p;
221,684✔
2564
  char* pStart1 = p1;
221,684✔
2565
  for (int32_t i = 0; i < numOfCols; ++i) {
944,508✔
2566
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
722,824✔
2567
    int32_t colLen1 = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength1[i]) : colLength1[i];
722,824✔
2568
    if (colLen >= dataLen) {
722,824✔
2569
      tscError("doConvertJson error: colLen:%d >= dataLen:%d", colLen, dataLen);
×
2570
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2571
    }
2572
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
722,824✔
2573
      int32_t* offset = (int32_t*)pStart;
256,755✔
2574
      int32_t* offset1 = (int32_t*)pStart1;
256,755✔
2575
      len = numOfRows * sizeof(int32_t);
256,755✔
2576
      (void)memcpy(pStart1, pStart, len);
256,755✔
2577
      pStart += len;
256,755✔
2578
      pStart1 += len;
256,755✔
2579
      totalLen += len;
256,755✔
2580

2581
      len = 0;
256,755✔
2582
      for (int32_t j = 0; j < numOfRows; ++j) {
1,230,641✔
2583
        if (offset[j] == -1) {
973,886✔
2584
          continue;
54,350✔
2585
        }
2586
        char* data = offset[j] + pStart;
919,536✔
2587

2588
        int32_t jsonInnerType = *data;
919,536✔
2589
        char*   jsonInnerData = data + CHAR_BYTES;
919,536✔
2590
        char    dst[TSDB_MAX_JSON_TAG_LEN] = {0};
919,536✔
2591
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
919,536✔
2592
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s", TSDB_DATA_NULL_STR_L);
12,744✔
2593
          varDataSetLen(dst, strlen(varDataVal(dst)));
12,744✔
2594
        } else if (tTagIsJson(data)) {
906,792✔
2595
          char* jsonString = NULL;
225,975✔
2596
          parseTagDatatoJson(data, &jsonString, pResultInfo->charsetCxt);
225,975✔
2597
          if (jsonString == NULL) {
225,975✔
2598
            tscError("doConvertJson error: parseTagDatatoJson failed");
×
2599
            return terrno;
×
2600
          }
2601
          STR_TO_VARSTR(dst, jsonString);
225,975✔
2602
          taosMemoryFree(jsonString);
225,975✔
2603
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
680,817✔
2604
          *(char*)varDataVal(dst) = '\"';
633,027✔
2605
          char    tmp[TSDB_MAX_JSON_TAG_LEN] = {0};
633,027✔
2606
          int32_t length = taosUcs4ToMbs((TdUcs4*)varDataVal(jsonInnerData), varDataLen(jsonInnerData), varDataVal(tmp),
633,027✔
2607
                                         pResultInfo->charsetCxt);
2608
          if (length <= 0) {
633,027✔
2609
            tscError("charset:%s to %s. convert failed.", DEFAULT_UNICODE_ENCODEC,
531✔
2610
                     pResultInfo->charsetCxt != NULL ? ((SConvInfo*)(pResultInfo->charsetCxt))->charset : tsCharset);
2611
            length = 0;
531✔
2612
          }
2613
          int32_t escapeLength = escapeToPrinted(varDataVal(dst) + CHAR_BYTES, TSDB_MAX_JSON_TAG_LEN - CHAR_BYTES * 2,
633,027✔
2614
                                                 varDataVal(tmp), length);
2615
          varDataSetLen(dst, escapeLength + CHAR_BYTES * 2);
633,027✔
2616
          *(char*)POINTER_SHIFT(varDataVal(dst), escapeLength + CHAR_BYTES) = '\"';
633,027✔
2617
          tscError("value:%s.", varDataVal(dst));
633,027✔
2618
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
47,790✔
2619
          double jsonVd = *(double*)(jsonInnerData);
35,046✔
2620
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%.9lf", jsonVd);
35,046✔
2621
          varDataSetLen(dst, strlen(varDataVal(dst)));
35,046✔
2622
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
12,744✔
2623
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s",
12,744✔
2624
                         (*((char*)jsonInnerData) == 1) ? "true" : "false");
12,744✔
2625
          varDataSetLen(dst, strlen(varDataVal(dst)));
12,744✔
2626
        } else {
2627
          tscError("doConvertJson error: invalid type:%d", jsonInnerType);
×
2628
          return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2629
        }
2630

2631
        offset1[j] = len;
919,536✔
2632
        (void)memcpy(pStart1 + len, dst, varDataTLen(dst));
919,536✔
2633
        len += varDataTLen(dst);
919,536✔
2634
      }
2635
      colLen1 = len;
256,755✔
2636
      totalLen += colLen1;
256,755✔
2637
      colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len;
256,755✔
2638
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
466,069✔
2639
      len = numOfRows * sizeof(int32_t);
63,760✔
2640
      (void)memcpy(pStart1, pStart, len);
63,760✔
2641
      pStart += len;
63,760✔
2642
      pStart1 += len;
63,760✔
2643
      totalLen += len;
63,760✔
2644
      totalLen += colLen;
63,760✔
2645
      (void)memcpy(pStart1, pStart, colLen);
63,760✔
2646
    } else {
2647
      len = BitmapLen(pResultInfo->numOfRows);
402,309✔
2648
      (void)memcpy(pStart1, pStart, len);
402,309✔
2649
      pStart += len;
402,309✔
2650
      pStart1 += len;
402,309✔
2651
      totalLen += len;
402,309✔
2652
      totalLen += colLen;
402,309✔
2653
      (void)memcpy(pStart1, pStart, colLen);
402,309✔
2654
    }
2655
    pStart += colLen;
722,824✔
2656
    pStart1 += colLen1;
722,824✔
2657
  }
2658

2659
  // Ensure the complete structure of the block, including the blankfill field,
2660
  // even though it is not used on the client side.
2661
  // (void)memcpy(pStart1, pStart, sizeof(bool));
2662
  totalLen += sizeof(bool);
221,684✔
2663

2664
  *(int32_t*)(pResultInfo->convertJson + 4) = totalLen;
221,684✔
2665
  pResultInfo->pData = pResultInfo->convertJson;
221,684✔
2666
  return TSDB_CODE_SUCCESS;
221,684✔
2667
}
2668

2669
int32_t setResultDataPtr(SReqResultInfo* pResultInfo, bool convertUcs4, bool isStmt) {
364,357,951✔
2670
  bool convertForDecimal = convertUcs4;
364,357,951✔
2671
  if (pResultInfo == NULL || pResultInfo->numOfCols <= 0 || pResultInfo->fields == NULL) {
364,357,951✔
2672
    tscError("setResultDataPtr paras error");
2,108✔
2673
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2674
  }
2675

2676
  if (pResultInfo->numOfRows == 0) {
364,358,231✔
2677
    return TSDB_CODE_SUCCESS;
25,496,928✔
2678
  }
2679

2680
  if (pResultInfo->pData == NULL) {
338,861,348✔
2681
    tscError("setResultDataPtr error: pData is NULL");
×
2682
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2683
  }
2684

2685
  int32_t code = doPrepareResPtr(pResultInfo);
338,860,396✔
2686
  if (code != TSDB_CODE_SUCCESS) {
338,857,725✔
2687
    return code;
×
2688
  }
2689
  code = doConvertJson(pResultInfo);
338,857,725✔
2690
  if (code != TSDB_CODE_SUCCESS) {
338,855,215✔
2691
    return code;
×
2692
  }
2693

2694
  char* p = (char*)pResultInfo->pData;
338,855,215✔
2695

2696
  // version:
2697
  int32_t blockVersion = *(int32_t*)p;
338,856,481✔
2698
  p += sizeof(int32_t);
338,857,701✔
2699

2700
  int32_t dataLen = *(int32_t*)p;
338,858,558✔
2701
  p += sizeof(int32_t);
338,858,149✔
2702

2703
  int32_t rows = *(int32_t*)p;
338,858,551✔
2704
  p += sizeof(int32_t);
338,859,261✔
2705

2706
  int32_t cols = *(int32_t*)p;
338,860,345✔
2707
  p += sizeof(int32_t);
338,861,712✔
2708

2709
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
338,860,473✔
2710
    tscError("setResultDataPtr paras error:rows;%d numOfRows:%" PRId64 " cols:%d numOfCols:%d", rows,
4,898✔
2711
             pResultInfo->numOfRows, cols, pResultInfo->numOfCols);
2712
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2713
  }
2714

2715
  int32_t hasColumnSeg = *(int32_t*)p;
338,858,970✔
2716
  p += sizeof(int32_t);
338,860,980✔
2717

2718
  uint64_t groupId = taosGetUInt64Aligned((uint64_t*)p);
338,863,402✔
2719
  p += sizeof(uint64_t);
338,863,402✔
2720

2721
  // check fields
2722
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
1,974,209,303✔
2723
    int8_t type = *(int8_t*)p;
1,635,360,473✔
2724
    p += sizeof(int8_t);
1,635,349,937✔
2725

2726
    int32_t bytes = *(int32_t*)p;
1,635,353,262✔
2727
    p += sizeof(int32_t);
1,635,354,889✔
2728

2729
    if (IS_DECIMAL_TYPE(type) && pResultInfo->fields[i].precision == 0) {
1,635,356,818✔
2730
      extractDecimalTypeInfoFromBytes(&bytes, &pResultInfo->fields[i].precision, &pResultInfo->fields[i].scale);
41,462✔
2731
    }
2732
  }
2733

2734
  int32_t* colLength = (int32_t*)p;
338,859,806✔
2735
  p += sizeof(int32_t) * pResultInfo->numOfCols;
338,859,806✔
2736

2737
  char* pStart = p;
338,860,191✔
2738
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
1,974,232,250✔
2739
    if ((pStart - pResultInfo->pData) >= dataLen) {
1,635,372,997✔
2740
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2741
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2742
    }
2743
    if (blockVersion == BLOCK_VERSION_1) {
1,635,361,243✔
2744
      colLength[i] = htonl(colLength[i]);
1,178,434,229✔
2745
    }
2746
    if (colLength[i] >= dataLen) {
1,635,362,256✔
2747
      tscError("invalid colLength %d, dataLen %d", colLength[i], dataLen);
×
2748
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2749
    }
2750
    if (IS_INVALID_TYPE(pResultInfo->fields[i].type)) {
1,635,367,369✔
2751
      tscError("invalid type %d", pResultInfo->fields[i].type);
5,111✔
2752
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2753
    }
2754
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,635,371,348✔
2755
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
350,123,144✔
2756
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
350,113,972✔
2757
    } else {
2758
      pResultInfo->pCol[i].nullbitmap = pStart;
1,285,270,136✔
2759
      pStart += BitmapLen(pResultInfo->numOfRows);
1,285,277,522✔
2760
    }
2761

2762
    pResultInfo->pCol[i].pData = pStart;
1,635,384,945✔
2763
    pResultInfo->length[i] =
2,147,483,647✔
2764
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
2,147,483,647✔
2765
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
1,635,373,131✔
2766

2767
    pStart += colLength[i];
1,635,374,807✔
2768
  }
2769

2770
  p = pStart;
338,863,738✔
2771
  // bool blankFill = *(bool*)p;
2772
  p += sizeof(bool);
338,863,738✔
2773
  int32_t offset = p - pResultInfo->pData;
338,864,147✔
2774
  if (offset > dataLen) {
338,863,498✔
2775
    tscError("invalid offset %d, dataLen %d", offset, dataLen);
×
2776
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2777
  }
2778

2779
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
2780
  if (convertUcs4) {
338,863,498✔
2781
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
338,690,793✔
2782
  }
2783
#endif
2784
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
338,863,026✔
2785
    code = convertDecimalType(pResultInfo);
338,690,287✔
2786
  }
2787
  return code;
338,860,288✔
2788
}
2789

2790
char* getDbOfConnection(STscObj* pObj) {
1,357,428,271✔
2791
  terrno = TSDB_CODE_SUCCESS;
1,357,428,271✔
2792
  char* p = NULL;
1,357,476,428✔
2793
  (void)taosThreadMutexLock(&pObj->mutex);
1,357,476,428✔
2794
  size_t len = strlen(pObj->db);
1,357,521,457✔
2795
  if (len > 0) {
1,357,522,636✔
2796
    p = taosStrndup(pObj->db, tListLen(pObj->db));
981,148,510✔
2797
    if (p == NULL) {
981,146,999✔
2798
      tscError("failed to taosStrndup db name");
×
2799
    }
2800
  }
2801

2802
  (void)taosThreadMutexUnlock(&pObj->mutex);
1,357,521,125✔
2803
  return p;
1,357,431,479✔
2804
}
2805

2806
void setConnectionDB(STscObj* pTscObj, const char* db) {
87,513,039✔
2807
  if (db == NULL || pTscObj == NULL) {
87,513,039✔
UNCOV
2808
    tscError("setConnectionDB para is NULL");
×
2809
    return;
×
2810
  }
2811

2812
  (void)taosThreadMutexLock(&pTscObj->mutex);
87,557,395✔
2813
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
87,580,549✔
2814
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
87,581,106✔
2815
}
2816

2817
void resetConnectDB(STscObj* pTscObj) {
×
2818
  if (pTscObj == NULL) {
×
2819
    return;
×
2820
  }
2821

2822
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
2823
  pTscObj->db[0] = 0;
×
2824
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
2825
}
2826

2827
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4,
282,349,883✔
2828
                              bool isStmt) {
2829
  if (pResultInfo == NULL || pRsp == NULL) {
282,349,883✔
2830
    tscError("setQueryResultFromRsp paras is null");
18✔
2831
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2832
  }
2833

2834
  taosMemoryFreeClear(pResultInfo->pRspMsg);
282,349,865✔
2835
  pResultInfo->pRspMsg = (const char*)pRsp;
282,349,885✔
2836
  pResultInfo->numOfRows = htobe64(pRsp->numOfRows);
282,349,915✔
2837
  pResultInfo->current = 0;
282,349,870✔
2838
  pResultInfo->completed = (pRsp->completed == 1);
282,349,904✔
2839
  pResultInfo->precision = pRsp->precision;
282,349,857✔
2840

2841
  // decompress data if needed
2842
  int32_t payloadLen = htonl(pRsp->payloadLen);
282,349,870✔
2843

2844
  if (pRsp->compressed) {
282,349,785✔
2845
    if (pResultInfo->decompBuf == NULL) {
×
2846
      pResultInfo->decompBuf = taosMemoryMalloc(payloadLen);
×
2847
      if (pResultInfo->decompBuf == NULL) {
×
2848
        tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2849
        return terrno;
×
2850
      }
2851
      pResultInfo->decompBufSize = payloadLen;
×
2852
    } else {
2853
      if (pResultInfo->decompBufSize < payloadLen) {
×
2854
        char* p = taosMemoryRealloc(pResultInfo->decompBuf, payloadLen);
×
2855
        if (p == NULL) {
×
2856
          tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2857
          return terrno;
×
2858
        }
2859

2860
        pResultInfo->decompBuf = p;
×
2861
        pResultInfo->decompBufSize = payloadLen;
×
2862
      }
2863
    }
2864
  }
2865

2866
  if (payloadLen > 0) {
282,349,803✔
2867
    int32_t compLen = *(int32_t*)pRsp->data;
256,854,063✔
2868
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
256,854,063✔
2869

2870
    char* pStart = (char*)pRsp->data + sizeof(int32_t) * 2;
256,854,063✔
2871

2872
    if (pRsp->compressed && compLen < rawLen) {
256,853,969✔
2873
      int32_t len = tsDecompressString(pStart, compLen, 1, pResultInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
×
2874
      if (len < 0) {
×
2875
        tscError("tsDecompressString failed");
×
2876
        return terrno ? terrno : TSDB_CODE_FAILED;
×
2877
      }
2878
      if (len != rawLen) {
×
2879
        tscError("tsDecompressString failed, len:%d != rawLen:%d", len, rawLen);
×
2880
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2881
      }
2882
      pResultInfo->pData = pResultInfo->decompBuf;
×
2883
      pResultInfo->payloadLen = rawLen;
×
2884
    } else {
2885
      pResultInfo->pData = pStart;
256,854,056✔
2886
      pResultInfo->payloadLen = htonl(pRsp->compLen);
256,854,063✔
2887
      if (pRsp->compLen != pRsp->payloadLen) {
256,854,065✔
2888
        tscError("pRsp->compLen:%d != pRsp->payloadLen:%d", pRsp->compLen, pRsp->payloadLen);
×
2889
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2890
      }
2891
    }
2892
  }
2893

2894
  // TODO handle the compressed case
2895
  pResultInfo->totalRows += pResultInfo->numOfRows;
282,349,772✔
2896

2897
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
282,349,859✔
2898
  return code;
282,348,330✔
2899
}
2900

2901
TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* details, int maxlen) {
271✔
2902
  TSDB_SERVER_STATUS code = TSDB_SRV_STATUS_UNAVAILABLE;
271✔
2903
  void*              clientRpc = NULL;
271✔
2904
  SServerStatusRsp   statusRsp = {0};
271✔
2905
  SEpSet             epSet = {.inUse = 0, .numOfEps = 1};
271✔
2906
  SRpcMsg  rpcMsg = {.info.ahandle = (void*)0x9527, .info.notFreeAhandle = 1, .msgType = TDMT_DND_SERVER_STATUS};
271✔
2907
  SRpcMsg  rpcRsp = {0};
271✔
2908
  SRpcInit rpcInit = {0};
271✔
2909
  char     pass[TSDB_PASSWORD_LEN + 1] = {0};
271✔
2910

2911
  rpcInit.label = "CHK";
271✔
2912
  rpcInit.numOfThreads = 1;
271✔
2913
  rpcInit.cfp = NULL;
271✔
2914
  rpcInit.sessions = 16;
271✔
2915
  rpcInit.connType = TAOS_CONN_CLIENT;
271✔
2916
  rpcInit.idleTime = tsShellActivityTimer * 1000;
271✔
2917
  rpcInit.compressSize = tsCompressMsgSize;
271✔
2918
  rpcInit.user = "_dnd";
271✔
2919

2920
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
271✔
2921
  connLimitNum = TMAX(connLimitNum, 10);
271✔
2922
  connLimitNum = TMIN(connLimitNum, 500);
271✔
2923
  rpcInit.connLimitNum = connLimitNum;
271✔
2924
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
271✔
2925
  rpcInit.readTimeout = tsReadTimeout;
271✔
2926
  rpcInit.ipv6 = tsEnableIpv6;
271✔
2927
  rpcInit.enableSSL = tsEnableTLS;
271✔
2928

2929
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
271✔
2930
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
271✔
2931
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
271✔
2932
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
271✔
2933
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
271✔
2934

2935
  if (TSDB_CODE_SUCCESS != taosVersionStrToInt(td_version, &rpcInit.compatibilityVer)) {
271✔
2936
    tscError("faild to convert taos version from str to int, errcode:%s", terrstr());
×
2937
    goto _OVER;
×
2938
  }
2939

2940
  clientRpc = rpcOpen(&rpcInit);
271✔
2941
  if (clientRpc == NULL) {
271✔
2942
    code = terrno;
×
2943
    tscError("failed to init server status client since %s", tstrerror(code));
×
2944
    goto _OVER;
×
2945
  }
2946

2947
  if (fqdn == NULL) {
271✔
2948
    fqdn = tsLocalFqdn;
271✔
2949
  }
2950

2951
  if (port == 0) {
271✔
2952
    port = tsServerPort;
271✔
2953
  }
2954

2955
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
271✔
2956
  epSet.eps[0].port = (uint16_t)port;
271✔
2957
  int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
271✔
2958
  if (TSDB_CODE_SUCCESS != ret) {
271✔
2959
    tscError("failed to send recv since %s", tstrerror(ret));
×
2960
    goto _OVER;
×
2961
  }
2962

2963
  if (rpcRsp.code != 0 || rpcRsp.contLen <= 0 || rpcRsp.pCont == NULL) {
271✔
2964
    tscError("failed to send server status req since %s", terrstr());
65✔
2965
    goto _OVER;
65✔
2966
  }
2967

2968
  if (tDeserializeSServerStatusRsp(rpcRsp.pCont, rpcRsp.contLen, &statusRsp) != 0) {
206✔
2969
    tscError("failed to parse server status rsp since %s", terrstr());
×
2970
    goto _OVER;
×
2971
  }
2972

2973
  code = statusRsp.statusCode;
206✔
2974
  if (details != NULL) {
206✔
2975
    tstrncpy(details, statusRsp.details, maxlen);
206✔
2976
  }
2977

2978
_OVER:
257✔
2979
  if (clientRpc != NULL) {
271✔
2980
    rpcClose(clientRpc);
271✔
2981
  }
2982
  if (rpcRsp.pCont != NULL) {
271✔
2983
    rpcFreeCont(rpcRsp.pCont);
206✔
2984
  }
2985
  return code;
271✔
2986
}
2987

2988
int32_t appendTbToReq(SHashObj* pHash, int32_t pos1, int32_t len1, int32_t pos2, int32_t len2, const char* str,
1,324✔
2989
                      int32_t acctId, char* db) {
2990
  SName name = {0};
1,324✔
2991

2992
  if (len1 <= 0) {
1,324✔
2993
    return -1;
×
2994
  }
2995

2996
  const char* dbName = db;
1,324✔
2997
  const char* tbName = NULL;
1,324✔
2998
  int32_t     dbLen = 0;
1,324✔
2999
  int32_t     tbLen = 0;
1,324✔
3000
  if (len2 > 0) {
1,324✔
3001
    dbName = str + pos1;
×
3002
    dbLen = len1;
×
3003
    tbName = str + pos2;
×
3004
    tbLen = len2;
×
3005
  } else {
3006
    dbLen = strlen(db);
1,324✔
3007
    tbName = str + pos1;
1,324✔
3008
    tbLen = len1;
1,324✔
3009
  }
3010

3011
  if (dbLen <= 0 || tbLen <= 0) {
1,324✔
3012
    return -1;
×
3013
  }
3014

3015
  if (tNameSetDbName(&name, acctId, dbName, dbLen)) {
1,324✔
3016
    return -1;
×
3017
  }
3018

3019
  if (tNameAddTbName(&name, tbName, tbLen)) {
1,324✔
3020
    return -1;
×
3021
  }
3022

3023
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
1,324✔
3024
  (void)snprintf(dbFName, TSDB_DB_FNAME_LEN, "%d.%.*s", acctId, dbLen, dbName);
1,324✔
3025

3026
  STablesReq* pDb = taosHashGet(pHash, dbFName, strlen(dbFName));
1,324✔
3027
  if (pDb) {
1,324✔
3028
    if (NULL == taosArrayPush(pDb->pTables, &name)) {
×
3029
      return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
3030
    }
3031
  } else {
3032
    STablesReq db;
1,324✔
3033
    db.pTables = taosArrayInit(20, sizeof(SName));
1,324✔
3034
    if (NULL == db.pTables) {
1,324✔
3035
      return terrno;
×
3036
    }
3037
    tstrncpy(db.dbFName, dbFName, TSDB_DB_FNAME_LEN);
1,324✔
3038
    if (NULL == taosArrayPush(db.pTables, &name)) {
2,648✔
3039
      return terrno;
×
3040
    }
3041
    TSC_ERR_RET(taosHashPut(pHash, dbFName, strlen(dbFName), &db, sizeof(db)));
1,324✔
3042
  }
3043

3044
  return TSDB_CODE_SUCCESS;
1,324✔
3045
}
3046

3047
int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq) {
1,324✔
3048
  SHashObj* pHash = taosHashInit(3, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
1,324✔
3049
  if (NULL == pHash) {
1,324✔
3050
    return terrno;
×
3051
  }
3052

3053
  bool    inEscape = false;
1,324✔
3054
  int32_t code = 0;
1,324✔
3055
  void*   pIter = NULL;
1,324✔
3056

3057
  int32_t vIdx = 0;
1,324✔
3058
  int32_t vPos[2];
1,324✔
3059
  int32_t vLen[2];
1,324✔
3060

3061
  (void)memset(vPos, -1, sizeof(vPos));
1,324✔
3062
  (void)memset(vLen, 0, sizeof(vLen));
1,324✔
3063

3064
  for (int32_t i = 0;; ++i) {
6,620✔
3065
    if (0 == *(tbList + i)) {
6,620✔
3066
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
1,324✔
3067
        vLen[vIdx] = i - vPos[vIdx];
1,324✔
3068
      }
3069

3070
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
1,324✔
3071
      if (code) {
1,324✔
3072
        goto _return;
×
3073
      }
3074

3075
      break;
1,324✔
3076
    }
3077

3078
    if ('`' == *(tbList + i)) {
5,296✔
3079
      inEscape = !inEscape;
×
3080
      if (!inEscape) {
×
3081
        if (vPos[vIdx] >= 0) {
×
3082
          vLen[vIdx] = i - vPos[vIdx];
×
3083
        } else {
3084
          goto _return;
×
3085
        }
3086
      }
3087

3088
      continue;
×
3089
    }
3090

3091
    if (inEscape) {
5,296✔
3092
      if (vPos[vIdx] < 0) {
×
3093
        vPos[vIdx] = i;
×
3094
      }
3095
      continue;
×
3096
    }
3097

3098
    if ('.' == *(tbList + i)) {
5,296✔
3099
      if (vPos[vIdx] < 0) {
×
3100
        goto _return;
×
3101
      }
3102
      if (vLen[vIdx] <= 0) {
×
3103
        vLen[vIdx] = i - vPos[vIdx];
×
3104
      }
3105
      vIdx++;
×
3106
      if (vIdx >= 2) {
×
3107
        goto _return;
×
3108
      }
3109
      continue;
×
3110
    }
3111

3112
    if (',' == *(tbList + i)) {
5,296✔
3113
      if (vPos[vIdx] < 0) {
×
3114
        goto _return;
×
3115
      }
3116
      if (vLen[vIdx] <= 0) {
×
3117
        vLen[vIdx] = i - vPos[vIdx];
×
3118
      }
3119

3120
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
×
3121
      if (code) {
×
3122
        goto _return;
×
3123
      }
3124

3125
      (void)memset(vPos, -1, sizeof(vPos));
×
3126
      (void)memset(vLen, 0, sizeof(vLen));
×
3127
      vIdx = 0;
×
3128
      continue;
×
3129
    }
3130

3131
    if (' ' == *(tbList + i) || '\r' == *(tbList + i) || '\t' == *(tbList + i) || '\n' == *(tbList + i)) {
5,296✔
3132
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
×
3133
        vLen[vIdx] = i - vPos[vIdx];
×
3134
      }
3135
      continue;
×
3136
    }
3137

3138
    if (('a' <= *(tbList + i) && 'z' >= *(tbList + i)) || ('A' <= *(tbList + i) && 'Z' >= *(tbList + i)) ||
5,296✔
3139
        ('0' <= *(tbList + i) && '9' >= *(tbList + i)) || ('_' == *(tbList + i))) {
662✔
3140
      if (vLen[vIdx] > 0) {
5,296✔
3141
        goto _return;
×
3142
      }
3143
      if (vPos[vIdx] < 0) {
5,296✔
3144
        vPos[vIdx] = i;
1,324✔
3145
      }
3146
      continue;
5,296✔
3147
    }
3148

3149
    goto _return;
×
3150
  }
3151

3152
  int32_t dbNum = taosHashGetSize(pHash);
1,324✔
3153
  *pReq = taosArrayInit(dbNum, sizeof(STablesReq));
1,324✔
3154
  if (NULL == pReq) {
1,324✔
3155
    TSC_ERR_JRET(terrno);
×
3156
  }
3157
  pIter = taosHashIterate(pHash, NULL);
1,324✔
3158
  while (pIter) {
2,648✔
3159
    STablesReq* pDb = (STablesReq*)pIter;
1,324✔
3160
    if (NULL == taosArrayPush(*pReq, pDb)) {
2,648✔
3161
      TSC_ERR_JRET(terrno);
×
3162
    }
3163
    pIter = taosHashIterate(pHash, pIter);
1,324✔
3164
  }
3165

3166
  taosHashCleanup(pHash);
1,324✔
3167

3168
  return TSDB_CODE_SUCCESS;
1,324✔
3169

3170
_return:
×
3171

3172
  terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
3173

3174
  pIter = taosHashIterate(pHash, NULL);
×
3175
  while (pIter) {
×
3176
    STablesReq* pDb = (STablesReq*)pIter;
×
3177
    taosArrayDestroy(pDb->pTables);
×
3178
    pIter = taosHashIterate(pHash, pIter);
×
3179
  }
3180

3181
  taosHashCleanup(pHash);
×
3182

3183
  return terrno;
×
3184
}
3185

3186
void syncCatalogFn(SMetaData* pResult, void* param, int32_t code) {
1,324✔
3187
  SSyncQueryParam* pParam = param;
1,324✔
3188
  pParam->pRequest->code = code;
1,324✔
3189

3190
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
1,324✔
3191
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
3192
  }
3193
}
1,324✔
3194

3195
void syncQueryFn(void* param, void* res, int32_t code) {
1,173,000,948✔
3196
  SSyncQueryParam* pParam = param;
1,173,000,948✔
3197
  pParam->pRequest = res;
1,173,000,948✔
3198

3199
  if (pParam->pRequest) {
1,173,029,140✔
3200
    pParam->pRequest->code = code;
1,173,016,949✔
3201
    clientOperateReport(pParam->pRequest);
1,173,037,716✔
3202
  }
3203

3204
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
1,172,937,424✔
3205
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
3206
  }
3207
}
1,173,086,816✔
3208

3209
void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
1,172,598,456✔
3210
                        int8_t source) {
3211
  if (sql == NULL || NULL == fp) {
1,172,598,456✔
3212
    terrno = TSDB_CODE_INVALID_PARA;
646✔
3213
    if (fp) {
×
3214
      fp(param, NULL, terrno);
×
3215
    }
3216

3217
    return;
196✔
3218
  }
3219

3220
  size_t sqlLen = strlen(sql);
1,172,603,284✔
3221
  if (sqlLen > (size_t)tsMaxSQLLength) {
1,172,603,284✔
3222
    tscError("conn:0x%" PRIx64 ", sql string exceeds max length:%d", connId, tsMaxSQLLength);
774✔
3223
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
774✔
3224
    fp(param, NULL, terrno);
774✔
3225
    return;
774✔
3226
  }
3227

3228
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, sql:%s", connId, sql);
1,172,602,510✔
3229

3230
  SRequestObj* pRequest = NULL;
1,172,600,081✔
3231
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, 0);
1,172,624,724✔
3232
  if (code != TSDB_CODE_SUCCESS) {
1,172,561,971✔
3233
    terrno = code;
4,649✔
3234
    fp(param, NULL, terrno);
4,649✔
3235
    return;
4,649✔
3236
  }
3237

3238
  code = connCheckAndUpateMetric(connId);
1,172,557,322✔
3239
  if (code != TSDB_CODE_SUCCESS) {
1,172,572,984✔
3240
    terrno = code;
532✔
3241
    fp(param, NULL, terrno);
532✔
3242
    return;
532✔
3243
  }
3244

3245
  pRequest->source = source;
1,172,572,452✔
3246
  pRequest->body.queryFp = fp;
1,172,572,323✔
3247
  doAsyncQuery(pRequest, false);
1,172,600,854✔
3248
}
3249

3250
void taosAsyncQueryImplWithReqid(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
196✔
3251
                                 int64_t reqid) {
3252
  if (sql == NULL || NULL == fp) {
196✔
3253
    terrno = TSDB_CODE_INVALID_PARA;
×
3254
    if (fp) {
×
3255
      fp(param, NULL, terrno);
×
3256
    }
3257

3258
    return;
6✔
3259
  }
3260

3261
  size_t sqlLen = strlen(sql);
196✔
3262
  if (sqlLen > (size_t)tsMaxSQLLength) {
196✔
3263
    tscError("conn:0x%" PRIx64 ", QID:0x%" PRIx64 ", sql string exceeds max length:%d", connId, reqid, tsMaxSQLLength);
×
3264
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
×
3265
    fp(param, NULL, terrno);
×
3266
    return;
×
3267
  }
3268

3269
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, QID:0x%" PRIx64 ", sql:%s", connId, reqid, sql);
196✔
3270

3271
  SRequestObj* pRequest = NULL;
196✔
3272
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, reqid);
196✔
3273
  if (code != TSDB_CODE_SUCCESS) {
196✔
3274
    terrno = code;
6✔
3275
    fp(param, NULL, terrno);
6✔
3276
    return;
6✔
3277
  }
3278

3279
  code = connCheckAndUpateMetric(connId);
190✔
3280

3281
  if (code != TSDB_CODE_SUCCESS) {
190✔
3282
    terrno = code;
×
3283
    fp(param, NULL, terrno);
×
3284
    return;
×
3285
  }
3286

3287
  pRequest->body.queryFp = fp;
190✔
3288

3289
  doAsyncQuery(pRequest, false);
190✔
3290
}
3291

3292
TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly, int8_t source) {
1,172,459,378✔
3293
  if (NULL == taos) {
1,172,459,378✔
3294
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3295
    return NULL;
×
3296
  }
3297

3298
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
1,172,459,378✔
3299
  if (NULL == param) {
1,172,490,443✔
3300
    return NULL;
×
3301
  }
3302

3303
  int32_t code = tsem_init(&param->sem, 0, 0);
1,172,490,443✔
3304
  if (TSDB_CODE_SUCCESS != code) {
1,172,450,350✔
3305
    taosMemoryFree(param);
×
3306
    return NULL;
×
3307
  }
3308

3309
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
1,172,450,350✔
3310
  code = tsem_wait(&param->sem);
1,172,393,665✔
3311
  if (TSDB_CODE_SUCCESS != code) {
1,172,484,235✔
3312
    taosMemoryFree(param);
×
3313
    return NULL;
×
3314
  }
3315
  code = tsem_destroy(&param->sem);
1,172,484,235✔
3316
  if (TSDB_CODE_SUCCESS != code) {
1,172,490,481✔
3317
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3318
  }
3319

3320
  SRequestObj* pRequest = NULL;
1,172,500,626✔
3321
  if (param->pRequest != NULL) {
1,172,500,626✔
3322
    param->pRequest->syncQuery = true;
1,172,492,761✔
3323
    pRequest = param->pRequest;
1,172,497,438✔
3324
    param->pRequest->inCallback = false;
1,172,495,273✔
3325
  }
3326
  taosMemoryFree(param);
1,172,501,248✔
3327

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

3331
  return pRequest;
1,172,495,625✔
3332
}
3333

3334
TAOS_RES* taosQueryImplWithReqid(TAOS* taos, const char* sql, bool validateOnly, int64_t reqid) {
196✔
3335
  if (NULL == taos) {
196✔
3336
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3337
    return NULL;
×
3338
  }
3339

3340
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
196✔
3341
  if (param == NULL) {
196✔
3342
    return NULL;
×
3343
  }
3344
  int32_t code = tsem_init(&param->sem, 0, 0);
196✔
3345
  if (TSDB_CODE_SUCCESS != code) {
196✔
3346
    taosMemoryFree(param);
×
3347
    return NULL;
×
3348
  }
3349

3350
  taosAsyncQueryImplWithReqid(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, reqid);
196✔
3351
  code = tsem_wait(&param->sem);
196✔
3352
  if (TSDB_CODE_SUCCESS != code) {
196✔
3353
    taosMemoryFree(param);
×
3354
    return NULL;
×
3355
  }
3356
  SRequestObj* pRequest = NULL;
196✔
3357
  if (param->pRequest != NULL) {
196✔
3358
    param->pRequest->syncQuery = true;
190✔
3359
    pRequest = param->pRequest;
190✔
3360
  }
3361
  taosMemoryFree(param);
196✔
3362

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

3366
  return pRequest;
196✔
3367
}
3368

3369
static void fetchCallback(void* pResult, void* param, int32_t code) {
278,543,459✔
3370
  SRequestObj* pRequest = (SRequestObj*)param;
278,543,459✔
3371

3372
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
278,543,459✔
3373

3374
  tscDebug("req:0x%" PRIx64 ", enter scheduler fetch cb, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
278,543,523✔
3375
           tstrerror(code), pRequest->requestId);
3376

3377
  pResultInfo->pData = pResult;
278,543,523✔
3378
  pResultInfo->numOfRows = 0;
278,543,521✔
3379

3380
  if (code != TSDB_CODE_SUCCESS) {
278,543,398✔
3381
    pRequest->code = code;
×
3382
    taosMemoryFreeClear(pResultInfo->pData);
×
3383
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
×
3384
    return;
×
3385
  }
3386

3387
  if (pRequest->code != TSDB_CODE_SUCCESS) {
278,543,398✔
3388
    taosMemoryFreeClear(pResultInfo->pData);
×
3389
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pRequest->code);
×
3390
    return;
×
3391
  }
3392

3393
  pRequest->code = setQueryResultFromRsp(pResultInfo, (const SRetrieveTableRsp*)pResultInfo->pData,
283,247,696✔
3394
                                         pResultInfo->convertUcs4, pRequest->stmtBindVersion > 0);
278,543,444✔
3395
  if (pRequest->code != TSDB_CODE_SUCCESS) {
278,542,371✔
3396
    pResultInfo->numOfRows = 0;
66✔
3397
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self,
66✔
3398
             tstrerror(pRequest->code), pRequest->requestId);
3399
  } else {
3400
    tscDebug(
278,542,231✔
3401
        "req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
3402
        pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed, pRequest->requestId);
3403

3404
    STscObj*            pTscObj = pRequest->pTscObj;
278,541,771✔
3405
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
278,543,391✔
3406
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
278,543,389✔
3407
  }
3408

3409
  pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows);
278,543,478✔
3410
}
3411

3412
void taosAsyncFetchImpl(SRequestObj* pRequest, __taos_async_fn_t fp, void* param) {
314,958,912✔
3413
  pRequest->body.fetchFp = fp;
314,958,912✔
3414
  ((SSyncQueryParam*)pRequest->body.interParam)->userParam = param;
314,958,912✔
3415

3416
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
314,958,843✔
3417

3418
  // this query has no results or error exists, return directly
3419
  if (taos_num_fields(pRequest) == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
314,958,843✔
3420
    pResultInfo->numOfRows = 0;
69✔
UNCOV
3421
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
×
3422
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
×
3423

3424
    return;
2,057,749✔
3425
  }
3426

3427
  // all data has returned to App already, no need to try again
3428
  if (pResultInfo->completed) {
314,958,843✔
3429
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
72,830,778✔
3430
    // it is a local executed query, no need to do async fetch
3431
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
36,415,389✔
3432
      if (pResultInfo->localResultFetched) {
1,681,218✔
3433
        pResultInfo->numOfRows = 0;
840,609✔
3434
        pResultInfo->current = 0;
840,609✔
3435
      } else {
3436
        pResultInfo->localResultFetched = true;
840,609✔
3437
      }
3438
    } else {
3439
      pResultInfo->numOfRows = 0;
34,734,171✔
3440
    }
3441

3442
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
36,415,389✔
3443
    return;
36,415,389✔
3444
  }
3445

3446
  SSchedulerReq req = {
278,543,454✔
3447
      .syncReq = false,
3448
      .fetchFp = fetchCallback,
3449
      .cbParam = pRequest,
3450
  };
3451

3452
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
278,543,523✔
3453
  if (TSDB_CODE_SUCCESS != code) {
278,542,942✔
3454
    tscError("0x%" PRIx64 " failed to schedule fetch rows", pRequest->requestId);
×
3455
    // pRequest->body.fetchFp(param, pRequest, code);
3456
  }
3457
}
3458

3459
void doRequestCallback(SRequestObj* pRequest, int32_t code) {
1,172,985,401✔
3460
  pRequest->inCallback = true;
1,172,985,401✔
3461

3462
  int64_t this = pRequest->self;
1,173,043,203✔
3463
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
1,172,975,206✔
3464
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
23,944✔
3465
    code = TSDB_CODE_SUCCESS;
×
3466
    pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
3467
    if (pRequest->code == TSDB_CODE_PAR_TABLE_NOT_EXIST || pRequest->code == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
3468
      pRequest->code = TSDB_CODE_SUCCESS;
2,431✔
3469
      if (pRequest->msgBuf != NULL && pRequest->msgBufLen > 0) {
×
3470
        pRequest->msgBuf[0] = '\0';
×
3471
      }
3472
    }
3473
  }
3474

3475
  tscDebug("QID:0x%" PRIx64 ", taos_query end, req:0x%" PRIx64 ", res:%p", pRequest->requestId, pRequest->self,
1,172,990,750✔
3476
           pRequest);
3477

3478
  if (pRequest->body.queryFp != NULL) {
1,172,991,188✔
3479
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
1,173,035,426✔
3480
  }
3481

3482
  SRequestObj* pReq = acquireRequest(this);
1,173,056,807✔
3483
  if (pReq != NULL) {
1,173,126,740✔
3484
    pReq->inCallback = false;
1,170,414,008✔
3485
    (void)releaseRequest(this);
1,170,414,428✔
3486
  }
3487
}
1,173,081,823✔
3488

3489
int32_t clientParseSql(void* param, const char* dbName, const char* sql, bool parseOnly, const char* effectiveUser,
570,059✔
3490
                       SParseSqlRes* pRes) {
3491
#ifndef TD_ENTERPRISE
3492
  return TSDB_CODE_SUCCESS;
3493
#else
3494
  return clientParseSqlImpl(param, dbName, sql, parseOnly, effectiveUser, pRes);
570,059✔
3495
#endif
3496
}
3497

3498
void updateConnAccessInfo(SConnAccessInfo* pInfo) {
1,260,257,319✔
3499
  if (pInfo == NULL) {
1,260,257,319✔
3500
    return;
×
3501
  }
3502
  int64_t ts = taosGetTimestampMs();
1,260,356,243✔
3503
  if (pInfo->startTime == 0) {
1,260,356,243✔
3504
    pInfo->startTime = ts;
87,753,214✔
3505
  }
3506
  pInfo->lastAccessTime = ts;
1,260,351,211✔
3507
}
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