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

taosdata / TDengine / #5049

11 May 2026 06:30AM UTC coverage: 73.313% (+0.09%) from 73.222%
#5049

push

travis-ci

web-flow
feat: refactor taosdump code to improve backup speed and compression ratio (#35292)

6625 of 8435 new or added lines in 28 files covered. (78.54%)

2491 existing lines in 142 files now uncovered.

281233 of 383605 relevant lines covered (73.31%)

132489999.79 hits per line

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

73.52
/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) {
704,961,999✔
40
  SRequestObj* pReq = acquireRequest(rId);
704,961,999✔
41
  if (pReq != NULL) {
704,999,060✔
42
    pReq->isQuery = true;
704,971,441✔
43
    (void)releaseRequest(rId);
704,969,524✔
44
  }
45
}
704,980,453✔
46

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

52
  size_t len = strlen(str);
181,556,958✔
53
  if (len <= 0 || len > maxsize) {
181,556,958✔
54
    return false;
432✔
55
  }
56

57
  return true;
181,558,446✔
58
}
59

60
static bool validateUserName(const char* user) { return stringLengthCheck(user, TSDB_USER_LEN - 1); }
90,548,024✔
61

62
static bool validatePassword(const char* passwd) { return stringLengthCheck(passwd, TSDB_PASSWORD_MAX_LEN); }
90,547,662✔
63

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

66
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
90,548,826✔
67
  char key[512] = {0};
90,548,826✔
68
  if (user == NULL) {
90,548,826✔
69
    (void)snprintf(key, sizeof(key), "%s:%s:%d", auth, ip, port);
2,691✔
70
  } else {
71
    (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
90,546,135✔
72
  }
73
  return taosStrdup(key);
90,548,826✔
74
}
75

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

81
  size_t escapeLength = 0;
635,959✔
82
  for (size_t i = 0; i < srcLength; ++i) {
18,029,276✔
83
    if (src[i] == '\"' || src[i] == '\\' || src[i] == '\b' || src[i] == '\f' || src[i] == '\n' || src[i] == '\r' ||
17,393,317✔
84
        src[i] == '\t') {
17,393,317✔
85
      escapeLength += 1;
×
86
    }
87
  }
88

89
  size_t dstLength = srcLength;
635,959✔
90
  if (escapeLength == 0) {
635,959✔
91
    (void)memcpy(dst, src, srcLength);
635,959✔
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;
635,959✔
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;
1,850✔
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,608,807✔
146
  taosHashCleanup(appInfo.pInstMap);
1,608,807✔
147
  taosHashCleanup(appInfo.pInstMapByClusterId);
1,608,807✔
148
  tscInfo("cluster instance map cleaned");
1,608,807✔
149
}
1,608,807✔
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,
90,550,858✔
156
                             uint16_t port, int connType, STscObj** pObj) {
157
  TSC_ERR_RET(taos_init());
90,550,858✔
158

159
  if (user == NULL) {
90,552,227✔
160
    if (auth == NULL || strlen(auth) != (TSDB_TOKEN_LEN - 1)) {
3,625✔
161
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_TOKEN);
934✔
162
    }
163
  } else if (!validateUserName(user)) {
90,548,602✔
164
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
165
  }
166
  int32_t code = 0;
90,550,148✔
167

168
  char localDb[TSDB_DB_NAME_LEN] = {0};
90,550,148✔
169
  if (db != NULL && strlen(db) > 0) {
90,550,205✔
170
    if (!validateDbName(db)) {
459,461✔
171
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
172
    }
173

174
    tstrncpy(localDb, db, sizeof(localDb));
458,793✔
175
    (void)strdequote(localDb);
458,141✔
176
  }
177

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

187
  SCorEpSet epSet = {0};
90,546,856✔
188
  if (ip) {
90,548,353✔
189
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
88,473,526✔
190
  } else {
191
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
2,074,827✔
192
  }
193

194
  if (port) {
90,549,144✔
195
    epSet.epSet.eps[0].port = port;
87,614,977✔
196
    epSet.epSet.eps[1].port = port;
87,614,977✔
197
  }
198

199
  char* key = getClusterKey(user, auth, ip, port);
90,549,144✔
200
  if (NULL == key) {
90,552,109✔
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,
90,552,109✔
204
          user ? user : "", db, key);
205
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
183,180,146✔
206
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
92,627,438✔
207
  }
208

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

249
    pInst = &p;
1,708,267✔
250
  } else {
251
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
88,844,357✔
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);
88,844,357✔
257
  }
258

259
_return:
90,552,822✔
260

261
  if (TSDB_CODE_SUCCESS != code) {
90,552,822✔
262
    (void)taosThreadMutexUnlock(&appInfo.mutex);
198✔
263
    taosMemoryFreeClear(key);
198✔
264
    return code;
198✔
265
  } else {
266
    code = taosThreadMutexUnlock(&appInfo.mutex);
90,552,624✔
267
    taosMemoryFreeClear(key);
90,549,294✔
268
    if (TSDB_CODE_SUCCESS != code) {
90,552,624✔
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);
90,552,624✔
273
  }
274
}
275

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

283
  taosEncryptPass_c((uint8_t*)pass, strlen(pass), auth);
90,550,110✔
284
  return taos_connect_by_auth(ip, user, auth, totp, db, port, connType, pObj);
90,548,411✔
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) {
576,112✔
297
  if (param == NULL) return;
576,112✔
298
  if (TSDB_CODE_SUCCESS != tsem_destroy(&param->sem)) {
576,112✔
299
    tscError("failed to destroy semaphore in freeQueryParam");
×
300
  }
301
  taosMemoryFree(param);
576,112✔
302
}
303

304
int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql,
1,106,606,987✔
305
                     SRequestObj** pRequest, int64_t reqid) {
306
  int32_t code = createRequest(connId, TSDB_SQL_SELECT, reqid, pRequest);
1,106,606,987✔
307
  if (TSDB_CODE_SUCCESS != code) {
1,106,593,806✔
308
    tscError("failed to malloc sqlObj, %s", sql);
1,301✔
309
    return code;
1,301✔
310
  }
311

312
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
1,106,592,505✔
313
  if ((*pRequest)->sqlstr == NULL) {
1,106,591,942✔
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,106,575,070✔
321
  (*pRequest)->sqlstr[sqlLen] = 0;
1,106,647,516✔
322
  (*pRequest)->sqlLen = sqlLen;
1,106,654,850✔
323
  (*pRequest)->validateOnly = validateSql;
1,106,653,570✔
324
  (*pRequest)->stmtBindVersion = 0;
1,106,648,494✔
325

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

335
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
1,106,526,193✔
336

337
  STscObj* pTscObj = (*pRequest)->pTscObj;
1,106,619,077✔
338
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
1,106,587,404✔
339
                             sizeof((*pRequest)->self));
340
  if (err) {
1,106,596,038✔
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,106,596,038✔
349
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
1,106,590,758✔
350
    if (TSDB_CODE_SUCCESS !=
484,814,394✔
351
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
484,798,893✔
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,106,696,104✔
361
  return TSDB_CODE_SUCCESS;
1,106,580,903✔
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,816,264✔
377
  STscObj* pTscObj = pRequest->pTscObj;
7,816,264✔
378

379
  SParseContext cxt = {
7,817,408✔
380
      .requestId = pRequest->requestId,
7,816,648✔
381
      .requestRid = pRequest->self,
7,814,630✔
382
      .acctId = pTscObj->acctId,
7,813,741✔
383
      .db = pRequest->pDb,
7,813,240✔
384
      .topicQuery = topicQuery,
385
      .pSql = pRequest->sqlstr,
7,815,222✔
386
      .sqlLen = pRequest->sqlLen,
7,817,152✔
387
      .pMsg = pRequest->msgBuf,
7,816,702✔
388
      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
389
      .pTransporter = pTscObj->pAppInfo->pTransporter,
7,812,886✔
390
      .pStmtCb = pStmtCb,
391
      .pUser = pTscObj->user,
7,813,736✔
392
      .userId = pTscObj->userId,
7,809,418✔
393
      .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
7,810,099✔
394
      .enableSysInfo = pTscObj->sysInfo,
7,812,054✔
395
      .minSecLevel = pTscObj->minSecLevel,
7,812,248✔
396
      .maxSecLevel = pTscObj->maxSecLevel,
7,811,381✔
397
      .macMode = pTscObj->pAppInfo->serverCfg.macActive,  // propagates cluster-level MAC state into parser/executor
7,812,672✔
398
      .sodInitial = pTscObj->pAppInfo->serverCfg.sodInitial,
7,814,215✔
399
      .svrVer = pTscObj->sVer,
7,817,973✔
400
      .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
7,817,609✔
401
      .stmtBindVersion = pRequest->stmtBindVersion,
7,815,950✔
402
      .setQueryFp = setQueryRequest,
403
      .timezone = pTscObj->optionInfo.timezone,
7,815,607✔
404
      .charsetCxt = pTscObj->optionInfo.charsetCxt,
7,814,814✔
405
  };
406

407
  cxt.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
7,814,689✔
408
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.pCatalog);
7,817,239✔
409
  if (code != TSDB_CODE_SUCCESS) {
7,814,323✔
410
    return code;
×
411
  }
412

413
  code = qParseSql(&cxt, pQuery);
7,814,323✔
414
  if (TSDB_CODE_SUCCESS == code) {
7,808,458✔
415
    if ((*pQuery)->haveResultSet) {
7,805,586✔
416
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols,
438✔
417
                              (*pQuery)->pResExtSchema, pRequest->stmtBindVersion > 0);
438✔
418
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
438✔
419
    }
420
  }
421

422
  if (TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) {
7,809,595✔
423
    TSWAP(pRequest->dbList, (*pQuery)->pDbList);
7,804,259✔
424
    TSWAP(pRequest->tableList, (*pQuery)->pTableList);
7,803,522✔
425
    TSWAP(pRequest->targetTableList, (*pQuery)->pTargetTableList);
7,804,336✔
426
  }
427

428
  taosArrayDestroy(cxt.pTableMetaPos);
7,796,674✔
429
  taosArrayDestroy(cxt.pTableVgroupPos);
7,800,054✔
430

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

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

450
  uint8_t mask = 0;
17,658✔
451
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_SYSTEM_SHOW)) mask |= SHOW_VAR_PRIV_SYSTEM;
17,658✔
452
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_SECURITY_SHOW)) mask |= SHOW_VAR_PRIV_SECURITY;
17,658✔
453
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_AUDIT_SHOW)) mask |= SHOW_VAR_PRIV_AUDIT;
17,658✔
454
  if (PRIV_HAS(&authRsp.sysPrivs, PRIV_VAR_DEBUG_SHOW)) mask |= SHOW_VAR_PRIV_DEBUG;
17,658✔
455
  return mask;
17,658✔
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) {
460,663✔
479
  // drop table if exists not_exists_table
480
  if (NULL == pQuery->pCmdMsg) {
460,663✔
481
    return TSDB_CODE_SUCCESS;
×
482
  }
483

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

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

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

498
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
1,847,453,459✔
499

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

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

520
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
6,739,346✔
521
  pRequest->code = code;
6,739,365✔
522

523
  if (pRequest->code != TSDB_CODE_SUCCESS) {
6,739,365✔
524
    pResultInfo->numOfRows = 0;
2,171✔
525
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
2,171✔
526
             pRequest->requestId);
527
  } else {
528
    tscDebug(
6,737,150✔
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,739,321✔
534
}
535

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

542
  // drop table if exists not_exists_table
543
  if (NULL == pQuery->pCmdMsg) {
106,878,713✔
544
    doRequestCallback(pRequest, 0);
7,933✔
545
    return TSDB_CODE_SUCCESS;
7,933✔
546
  }
547

548
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
106,870,780✔
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;
106,870,780✔
552
  pRequest->type = pMsgInfo->msgType;
106,870,288✔
553
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
106,870,295✔
554
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
106,870,602✔
555

556
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
106,870,285✔
557
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
106,869,694✔
558

559
  int32_t code = asyncSendMsgToServer(pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg);
106,875,643✔
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);
106,883,107✔
565
  if (code) {
106,884,716✔
566
    doRequestCallback(pRequest, code);
×
567
  }
568
  return code;
106,885,639✔
569
}
570

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

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

579
  return node1->load > node2->load;
187,237✔
580
}
581

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

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

598
  return TSDB_CODE_SUCCESS;
382,762✔
599
}
600

601
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
1,099,622,147✔
602
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
1,099,622,147✔
603
    *required = false;
1,086,120,758✔
604
    return TSDB_CODE_SUCCESS;
1,086,095,305✔
605
  }
606

607
  int32_t       code = TSDB_CODE_SUCCESS;
13,501,389✔
608
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
13,501,389✔
609
  *required = false;
13,500,836✔
610

611
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
13,501,942✔
612
  *required = (NULL == pInfo->pQnodeList);
13,501,942✔
613
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
13,501,389✔
614
  return TSDB_CODE_SUCCESS;
13,501,942✔
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) {
9,207,802✔
650
  pRequest->type = pQuery->msgType;
9,207,802✔
651
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
9,204,448✔
652

653
  SPlanContext cxt = {.queryId = pRequest->requestId,
9,583,478✔
654
                      .acctId = pRequest->pTscObj->acctId,
9,208,022✔
655
                      .mgmtEpSet = getEpSet_s(&pAppInfo->mgmtEp),
9,201,865✔
656
                      .pAstRoot = pQuery->pRoot,
9,217,386✔
657
                      .showRewrite = pQuery->showRewrite,
9,211,349✔
658
                      .pMsg = pRequest->msgBuf,
9,210,146✔
659
                      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
660
                      .pUser = pRequest->pTscObj->user,
9,207,045✔
661
                      .userId = pRequest->pTscObj->userId,
9,201,244✔
662
                      .timezone = pRequest->pTscObj->optionInfo.timezone,
9,204,887✔
663
                      .sysInfo = pRequest->pTscObj->sysInfo,
9,205,852✔
664
                      .minSecLevel = pRequest->pTscObj->minSecLevel,
9,210,959✔
665
                      .maxSecLevel = pRequest->pTscObj->maxSecLevel,
9,198,200✔
666
                      .macMode = pAppInfo->serverCfg.macActive};
9,205,017✔
667

668
  return qCreateQueryPlan(&cxt, pPlan, pNodeList);
9,195,625✔
669
}
670

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

678
  pResInfo->numOfCols = numOfCols;
298,226,707✔
679
  if (pResInfo->fields != NULL) {
298,225,804✔
680
    taosMemoryFree(pResInfo->fields);
16,868✔
681
  }
682
  if (pResInfo->userFields != NULL) {
298,221,337✔
683
    taosMemoryFree(pResInfo->userFields);
16,868✔
684
  }
685
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
298,226,387✔
686
  if (NULL == pResInfo->fields) return terrno;
298,212,563✔
687
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
298,215,229✔
688
  if (NULL == pResInfo->userFields) {
298,207,283✔
689
    taosMemoryFree(pResInfo->fields);
×
690
    return terrno;
×
691
  }
692
  if (numOfCols != pResInfo->numOfCols) {
298,210,243✔
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,533,974,938✔
698
    pResInfo->fields[i].type = pSchema[i].type;
1,235,748,214✔
699

700
    pResInfo->userFields[i].type = pSchema[i].type;
1,235,752,389✔
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,235,755,012✔
703
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
1,235,752,819✔
704
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
1,235,755,698✔
705
      decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale);
1,570,944✔
706
    }
707

708
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
1,235,758,101✔
709
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
1,235,770,391✔
710
  }
711
  return TSDB_CODE_SUCCESS;
298,238,344✔
712
}
713

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

720
  pResInfo->precision = precision;
213,646,347✔
721
}
722

723
int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) {
218,495,971✔
724
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
218,495,971✔
725
  if (NULL == nodeList) {
218,512,714✔
726
    return terrno;
×
727
  }
728
  char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client";
218,515,395✔
729

730
  int32_t dbNum = taosArrayGetSize(pDbVgList);
218,515,395✔
731
  for (int32_t i = 0; i < dbNum; ++i) {
434,325,775✔
732
    SArray* pVg = taosArrayGetP(pDbVgList, i);
215,769,593✔
733
    if (NULL == pVg) {
215,783,630✔
734
      continue;
×
735
    }
736
    int32_t vgNum = taosArrayGetSize(pVg);
215,783,630✔
737
    if (vgNum <= 0) {
215,781,407✔
738
      continue;
672,789✔
739
    }
740

741
    for (int32_t j = 0; j < vgNum; ++j) {
729,198,864✔
742
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
514,074,336✔
743
      if (NULL == pInfo) {
514,092,335✔
744
        taosArrayDestroy(nodeList);
×
745
        return TSDB_CODE_OUT_OF_RANGE;
×
746
      }
747
      SQueryNodeLoad load = {0};
514,092,335✔
748
      load.addr.nodeId = pInfo->vgId;
514,081,742✔
749
      load.addr.epSet = pInfo->epSet;
514,094,179✔
750

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

758
  int32_t vnodeNum = taosArrayGetSize(nodeList);
218,556,182✔
759
  if (vnodeNum > 0) {
218,537,239✔
760
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
214,765,089✔
761
    goto _return;
214,760,392✔
762
  }
763

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

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

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

782
_return:
138,734✔
783

784
  *pNodeList = nodeList;
218,526,471✔
785

786
  return TSDB_CODE_SUCCESS;
218,522,318✔
787
}
788

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

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

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

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

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

828
_return:
×
829

830
  *pNodeList = nodeList;
1,708,245✔
831

832
  return TSDB_CODE_SUCCESS;
1,708,245✔
833
}
834

835
void freeVgList(void* list) {
9,075,735✔
836
  SArray* pList = *(SArray**)list;
9,075,735✔
837
  taosArrayDestroy(pList);
9,079,783✔
838
}
9,074,414✔
839

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

846
  switch (tsQueryPolicy) {
211,001,117✔
847
    case QUERY_POLICY_VNODE:
209,306,589✔
848
    case QUERY_POLICY_CLIENT: {
849
      if (pResultMeta) {
209,306,589✔
850
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
209,312,992✔
851
        if (NULL == pDbVgList) {
209,312,994✔
852
          code = terrno;
×
853
          goto _return;
×
854
        }
855
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
209,312,994✔
856
        for (int32_t i = 0; i < dbNum; ++i) {
416,008,068✔
857
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
206,699,405✔
858
          if (pRes->code || NULL == pRes->pRes) {
206,697,700✔
859
            continue;
664✔
860
          }
861

862
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
413,392,872✔
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);
209,310,445✔
907
      break;
209,307,356✔
908
    }
909
    case QUERY_POLICY_HYBRID:
1,708,245✔
910
    case QUERY_POLICY_QNODE: {
911
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
1,747,536✔
912
        SMetaRes* pRes = taosArrayGet(pResultMeta->pQnodeList, 0);
39,291✔
913
        if (pRes->code) {
39,291✔
914
          pQnodeList = NULL;
×
915
        } else {
916
          pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL);
39,291✔
917
          if (NULL == pQnodeList) {
39,291✔
918
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
919
            goto _return;
×
920
          }
921
        }
922
      } else {
923
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
1,668,954✔
924
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
1,668,954✔
925
        if (pInst->pQnodeList) {
1,668,954✔
926
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
1,668,954✔
927
          if (NULL == pQnodeList) {
1,668,954✔
928
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
929
            goto _return;
×
930
          }
931
        }
932
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
1,668,954✔
933
      }
934

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

943
_return:
211,015,601✔
944
  taosArrayDestroyEx(pDbVgList, fp);
211,015,601✔
945
  taosArrayDestroy(pQnodeList);
211,018,849✔
946

947
  return code;
211,020,922✔
948
}
949

950
int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList) {
9,202,102✔
951
  SArray* pDbVgList = NULL;
9,202,102✔
952
  SArray* pQnodeList = NULL;
9,202,102✔
953
  int32_t code = 0;
9,204,366✔
954

955
  switch (tsQueryPolicy) {
9,204,366✔
956
    case QUERY_POLICY_VNODE:
9,197,199✔
957
    case QUERY_POLICY_CLIENT: {
958
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
9,197,199✔
959
      if (dbNum > 0) {
9,209,550✔
960
        SCatalog*     pCtg = NULL;
9,078,735✔
961
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
9,077,525✔
962
        code = catalogGetHandle(pInst->clusterId, &pCtg);
9,074,901✔
963
        if (code != TSDB_CODE_SUCCESS) {
9,076,232✔
964
          goto _return;
×
965
        }
966

967
        pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
9,076,232✔
968
        if (NULL == pDbVgList) {
9,075,592✔
969
          code = terrno;
×
970
          goto _return;
×
971
        }
972
        SArray* pVgList = NULL;
9,075,628✔
973
        for (int32_t i = 0; i < dbNum; ++i) {
18,151,941✔
974
          char*            dbFName = taosArrayGet(pRequest->dbList, i);
9,070,876✔
975
          SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
9,076,709✔
976
                                   .requestId = pRequest->requestId,
9,077,689✔
977
                                   .requestObjRefId = pRequest->self,
9,072,787✔
978
                                   .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
9,072,177✔
979

980
          // catalogGetDBVgList will handle dbFName == null.
981
          code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
9,085,382✔
982
          if (code) {
9,074,690✔
983
            goto _return;
×
984
          }
985

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

993
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
9,214,033✔
994
      break;
9,207,654✔
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,167✔
1004
      tscError("unknown query policy: %d", tsQueryPolicy);
7,167✔
1005
      return TSDB_CODE_APP_ERROR;
×
1006
  }
1007

1008
_return:
9,208,422✔
1009

1010
  taosArrayDestroyEx(pDbVgList, freeVgList);
9,202,673✔
1011
  taosArrayDestroy(pQnodeList);
9,200,599✔
1012

1013
  return code;
9,207,676✔
1014
}
1015

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

1019
  SExecResult      res = {0};
9,212,763✔
1020
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
9,210,878✔
1021
                           .requestId = pRequest->requestId,
9,208,391✔
1022
                           .requestObjRefId = pRequest->self};
9,201,022✔
1023
  SSchedulerReq    req = {
9,571,347✔
1024
         .syncReq = true,
1025
         .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
9,197,076✔
1026
         .pConn = &conn,
1027
         .pNodeList = pNodeList,
1028
         .pDag = pDag,
1029
         .sql = pRequest->sqlstr,
9,197,076✔
1030
         .startTs = pRequest->metric.start,
9,200,612✔
1031
         .execFp = NULL,
1032
         .cbParam = NULL,
1033
         .chkKillFp = chkRequestKilled,
1034
         .chkKillParam = (void*)pRequest->self,
9,196,782✔
1035
         .pExecRes = &res,
1036
         .source = pRequest->source,
9,210,441✔
1037
         .secureDelete = pRequest->secureDelete,
9,202,336✔
1038
         .pWorkerCb = getTaskPoolWorkerCb(),
9,209,066✔
1039
  };
1040

1041
  int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob);
9,205,850✔
1042

1043
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
9,218,499✔
1044
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
9,219,125✔
1045

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

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

1054
  if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_DELETE == pRequest->type ||
9,217,535✔
1055
      TDMT_VND_CREATE_TABLE == pRequest->type) {
101,429✔
1056
    pRequest->body.resInfo.numOfRows = res.numOfRows;
9,177,780✔
1057
    if (TDMT_VND_SUBMIT == pRequest->type) {
9,177,780✔
1058
      STscObj*            pTscObj = pRequest->pTscObj;
9,114,466✔
1059
      SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
9,114,466✔
1060
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows);
9,117,375✔
1061
    }
1062

1063
    schedulerFreeJob(&pRequest->body.queryJob, 0);
9,178,689✔
1064
  }
1065

1066
  pRequest->code = res.code;
9,216,106✔
1067
  terrno = res.code;
9,218,461✔
1068
  return pRequest->code;
9,214,870✔
1069
}
1070

1071
int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
621,639,675✔
1072
  SArray*      pArray = NULL;
621,639,675✔
1073
  SSubmitRsp2* pRsp = (SSubmitRsp2*)res;
621,639,675✔
1074
  if (NULL == pRsp->aCreateTbRsp) {
621,639,675✔
1075
    return TSDB_CODE_SUCCESS;
607,435,487✔
1076
  }
1077

1078
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
14,236,613✔
1079
  for (int32_t i = 0; i < tbNum; ++i) {
31,838,856✔
1080
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
17,600,580✔
1081
    if (pTbRsp->pMeta) {
17,600,704✔
1082
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
17,262,814✔
1083
    }
1084
  }
1085

1086
  return TSDB_CODE_SUCCESS;
14,238,276✔
1087
}
1088

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

1098
  pArray = taosArrayInit(tbNum, sizeof(STbSVersion));
168,556,493✔
1099
  if (NULL == pArray) {
168,558,650✔
1100
    return terrno;
36✔
1101
  }
1102

1103
  for (int32_t i = 0; i < tbNum; ++i) {
540,487,721✔
1104
    STbVerInfo* tbInfo = taosArrayGet(pTbArray, i);
371,929,340✔
1105
    if (NULL == tbInfo) {
371,929,767✔
1106
      code = terrno;
×
1107
      goto _return;
×
1108
    }
1109
    STbSVersion tbSver = {
371,929,767✔
1110
        .tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion, .rver = tbInfo->rversion};
371,929,162✔
1111
    if (NULL == taosArrayPush(pArray, &tbSver)) {
371,929,852✔
1112
      code = terrno;
×
1113
      goto _return;
×
1114
    }
1115
  }
1116

1117
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
168,558,381✔
1118
                           .requestId = pRequest->requestId,
168,559,412✔
1119
                           .requestObjRefId = pRequest->self,
168,559,258✔
1120
                           .mgmtEps = *epset};
1121

1122
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
168,558,376✔
1123

1124
_return:
168,558,833✔
1125

1126
  taosArrayDestroy(pArray);
168,557,032✔
1127
  return code;
168,558,722✔
1128
}
1129

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

1134
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog) {
75,362,863✔
1135
  return catalogAsyncUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
75,362,863✔
1136
}
1137

1138
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
898,432,253✔
1139
  if (NULL == pRequest->body.resInfo.execRes.res) {
898,432,253✔
1140
    return pRequest->code;
55,742,763✔
1141
  }
1142

1143
  SCatalog*     pCatalog = NULL;
842,632,518✔
1144
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
842,644,961✔
1145

1146
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
842,700,541✔
1147
  if (code) {
842,676,824✔
1148
    return code;
×
1149
  }
1150

1151
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
842,676,824✔
1152
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
842,728,644✔
1153

1154
  switch (pRes->msgType) {
842,735,293✔
1155
    case TDMT_VND_ALTER_TABLE:
4,218,409✔
1156
    case TDMT_MND_ALTER_STB: {
1157
      code = handleAlterTbExecRes(pRes->res, pCatalog);
4,218,409✔
1158
      break;
4,218,409✔
1159
    }
1160
    case TDMT_VND_CREATE_TABLE: {
47,820,321✔
1161
      SArray* pList = (SArray*)pRes->res;
47,820,321✔
1162
      int32_t num = taosArrayGetSize(pList);
47,827,247✔
1163
      for (int32_t i = 0; i < num; ++i) {
103,685,015✔
1164
        void* res = taosArrayGetP(pList, i);
55,848,579✔
1165
        // handleCreateTbExecRes will handle res == null
1166
        code = handleCreateTbExecRes(res, pCatalog);
55,850,104✔
1167
      }
1168
      break;
47,836,436✔
1169
    }
1170
    case TDMT_MND_CREATE_STB: {
433,806✔
1171
      code = handleCreateTbExecRes(pRes->res, pCatalog);
433,806✔
1172
      break;
433,806✔
1173
    }
1174
    case TDMT_VND_SUBMIT: {
621,630,372✔
1175
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
621,630,372✔
1176

1177
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
621,676,024✔
1178
      break;
621,648,408✔
1179
    }
1180
    case TDMT_SCH_QUERY:
168,557,970✔
1181
    case TDMT_SCH_MERGE_QUERY: {
1182
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
168,557,970✔
1183
      break;
168,574,296✔
1184
    }
1185
    default:
692✔
1186
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self,
692✔
1187
               pRequest->type, pRequest->requestId);
1188
      code = TSDB_CODE_APP_ERROR;
×
1189
  }
1190

1191
  return code;
842,711,355✔
1192
}
1193

1194
static bool incompletaFileParsing(SNode* pStmt) {
875,719,601✔
1195
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
875,719,601✔
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) {
77,433,184✔
1217
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
77,433,184✔
1218
    // return to client
1219
    doRequestCallback(pRequest, pRequest->code);
77,433,184✔
1220
    return;
77,429,216✔
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) {
888,869,548✔
1340
  SSqlCallbackWrapper* pWrapper = param;
888,869,548✔
1341
  SRequestObj*         pRequest = pWrapper->pRequest;
888,869,548✔
1342
  STscObj*             pTscObj = pRequest->pTscObj;
888,888,106✔
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;
888,899,925✔
1349
  if (pResult) {
888,901,742✔
1350
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
888,790,311✔
1351
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
888,797,627✔
1352
  }
1353

1354
  int32_t type = pRequest->type;
888,845,911✔
1355
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
888,798,547✔
1356
    if (pResult) {
664,659,931✔
1357
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
664,643,108✔
1358

1359
      // record the insert rows
1360
      if (TDMT_VND_SUBMIT == type) {
664,686,976✔
1361
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
613,209,884✔
1362
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
613,226,873✔
1363
      }
1364
    }
1365
    schedulerFreeJob(&pRequest->body.queryJob, 0);
664,705,369✔
1366
  }
1367

1368
  taosMemoryFree(pResult);
888,895,790✔
1369
  tscDebug("req:0x%" PRIx64 ", enter scheduler exec cb, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
888,850,275✔
1370
           pRequest->requestId);
1371

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

1383
  tscTrace("req:0x%" PRIx64 ", scheduler exec cb, request type:%s", pRequest->self, TMSG_INFO(pRequest->type));
888,742,483✔
1384
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
888,742,483✔
1385
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
3,770,751✔
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;
888,767,336✔
1391

1392
  int32_t code1 = handleQueryExecRsp(pRequest);
888,780,134✔
1393
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
888,771,208✔
1394
    pRequest->code = code1;
×
1395
  }
1396

1397
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
1,764,520,481✔
1398
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
875,692,622✔
1399
    continueInsertFromCsv(pWrapper, pRequest);
13,444✔
1400
    return;
13,444✔
1401
  }
1402

1403
  if (pRequest->relation.nextRefId) {
888,828,023✔
1404
    handlePostSubQuery(pWrapper);
×
1405
  } else {
1406
    destorySqlCallbackWrapper(pWrapper);
888,800,885✔
1407
    pRequest->pWrapper = NULL;
888,717,303✔
1408

1409
    // return to client
1410
    doRequestCallback(pRequest, code);
888,734,719✔
1411
  }
1412
}
1413

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

1418
  if (pQuery->pRoot) {
9,666,274✔
1419
    pRequest->stmtType = pQuery->pRoot->type;
9,213,504✔
1420
    if (nodeType(pQuery->pRoot) == QUERY_NODE_DELETE_STMT) {
9,200,828✔
1421
      pRequest->secureDelete = ((SDeleteStmt*)pQuery->pRoot)->secureDelete;
×
1422
    }
1423
  }
1424

1425
  if (pQuery->pRoot && !pRequest->inRetry) {
9,673,964✔
1426
    STscObj*            pTscObj = pRequest->pTscObj;
9,206,211✔
1427
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
9,210,117✔
1428
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) {
9,214,372✔
1429
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
9,182,193✔
1430
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
22,964✔
1431
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
22,971✔
1432
    }
1433
  }
1434

1435
  pRequest->body.execMode = pQuery->execMode;
9,681,405✔
1436
  switch (pQuery->execMode) {
9,665,013✔
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:
460,663✔
1448
      if (!pRequest->validateOnly) {
460,663✔
1449
        code = execDdlQuery(pRequest, pQuery);
460,663✔
1450
      }
1451
      break;
460,663✔
1452
    case QUERY_EXEC_MODE_SCHEDULE: {
9,194,669✔
1453
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
9,194,669✔
1454
      if (NULL == pMnodeList) {
9,199,284✔
1455
        code = terrno;
×
1456
        break;
×
1457
      }
1458
      SQueryPlan* pDag = NULL;
9,199,284✔
1459
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
9,200,812✔
1460
      if (TSDB_CODE_SUCCESS == code) {
9,209,575✔
1461
        pRequest->body.subplanNum = pDag->numOfSubplans;
9,215,204✔
1462
        if (!pRequest->validateOnly) {
9,206,620✔
1463
          SArray* pNodeList = NULL;
9,196,363✔
1464
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
9,198,312✔
1465

1466
          if (TSDB_CODE_SUCCESS == code) {
9,210,946✔
1467
            code = sessMetricCheckValue((SSessMetric*)pRequest->pTscObj->pSessMetric, SESSION_MAX_CALL_VNODE_NUM,
9,213,314✔
1468
                                        taosArrayGetSize(pNodeList));
9,213,745✔
1469
          }
1470

1471
          if (TSDB_CODE_SUCCESS == code) {
9,205,330✔
1472
            code = scheduleQuery(pRequest, pDag, pNodeList);
9,205,330✔
1473
          }
1474
          taosArrayDestroy(pNodeList);
9,213,714✔
1475
        }
1476
      }
1477
      taosArrayDestroy(pMnodeList);
9,213,213✔
1478
      break;
9,214,139✔
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,674,246✔
1488
    qDestroyQuery(pQuery);
×
1489
  }
1490

1491
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
9,674,246✔
1492
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
28,625✔
1493
    if (TSDB_CODE_SUCCESS != ret) {
28,612✔
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,674,918✔
1500
    code = handleQueryExecRsp(pRequest);
9,672,351✔
1501
  }
1502

1503
  if (TSDB_CODE_SUCCESS != code) {
9,678,887✔
1504
    pRequest->code = code;
8,235✔
1505
  }
1506

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

1513
static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta,
889,049,827✔
1514
                                 SSqlCallbackWrapper* pWrapper) {
1515
  int32_t code = TSDB_CODE_SUCCESS;
889,049,827✔
1516
  pRequest->type = pQuery->msgType;
889,049,827✔
1517
  SArray*     pMnodeList = NULL;
889,188,410✔
1518
  SArray*     pNodeList = NULL;
889,188,410✔
1519
  SQueryPlan* pDag = NULL;
889,170,385✔
1520
  int64_t     st = taosGetTimestampUs();
889,205,763✔
1521

1522
  if (!pRequest->parseOnly) {
889,205,763✔
1523
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_PLAN);
1,778,431,647✔
1524

1525
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
889,411,517✔
1526
    if (NULL == pMnodeList) {
889,200,367✔
1527
      code = terrno;
×
1528
    }
1529
    SPlanContext cxt = {.queryId = pRequest->requestId,
977,023,835✔
1530
                        .acctId = pRequest->pTscObj->acctId,
889,295,501✔
1531
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
889,339,308✔
1532
                        .pAstRoot = pQuery->pRoot,
889,383,708✔
1533
                        .showRewrite = pQuery->showRewrite,
889,395,028✔
1534
                        .isView = pWrapper->pParseCtx->isView,
889,369,646✔
1535
                        .isAudit = pWrapper->pParseCtx->isAudit,
889,364,185✔
1536
                        .privInfo = pWrapper->pParseCtx->privInfo,
889,254,991✔
1537
                        .pMsg = pRequest->msgBuf,
889,317,608✔
1538
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1539
                        .pUser = pRequest->pTscObj->user,
889,308,385✔
1540
                        .userId = pRequest->pTscObj->userId,
889,286,937✔
1541
                        .sysInfo = pRequest->pTscObj->sysInfo,
889,246,022✔
1542
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
889,247,084✔
1543
                        .allocatorId = pRequest->stmtBindVersion > 0 ? 0 : pRequest->allocatorRefId};
889,224,694✔
1544
    if (TSDB_CODE_SUCCESS == code) {
889,282,546✔
1545
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
889,263,830✔
1546
    }
1547
    if (code) {
889,221,831✔
1548
      tscError("req:0x%" PRIx64 " requestId:0x%" PRIx64 ", failed to create query plan, code:%s msg:%s", pRequest->self,
279,031✔
1549
               pRequest->requestId, tstrerror(code), cxt.pMsg);
1550
    } else {
1551
      pRequest->body.subplanNum = pDag->numOfSubplans;
888,942,800✔
1552
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
889,016,334✔
1553
    }
1554
  }
1555

1556
  pRequest->metric.execStart = taosGetTimestampUs();
889,245,725✔
1557
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
889,274,099✔
1558

1559
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
889,229,676✔
1560
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
888,588,141✔
1561
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_SCHEDULE);
422,053,313✔
1562
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
211,034,380✔
1563
    }
1564

1565
    if (code == TSDB_CODE_SUCCESS) {
888,763,038✔
1566
      code = sessMetricCheckValue((SSessMetric*)pRequest->pTscObj->pSessMetric, SESSION_MAX_CALL_VNODE_NUM,
888,667,658✔
1567
                                  taosArrayGetSize(pNodeList));
888,656,333✔
1568
    }
1569

1570
    if (code == TSDB_CODE_SUCCESS) {
888,800,794✔
1571
      SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
888,797,234✔
1572
                               .requestId = pRequest->requestId,
888,840,250✔
1573
                               .requestObjRefId = pRequest->self};
888,816,078✔
1574
      SSchedulerReq    req = {
932,697,447✔
1575
             .syncReq = false,
1576
             .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
888,749,378✔
1577
             .pConn = &conn,
1578
             .pNodeList = pNodeList,
1579
             .pDag = pDag,
1580
             .allocatorRefId = pRequest->allocatorRefId,
888,749,378✔
1581
             .sql = pRequest->sqlstr,
888,708,086✔
1582
             .startTs = pRequest->metric.start,
888,720,539✔
1583
             .execFp = schedulerExecCb,
1584
             .cbParam = pWrapper,
1585
             .chkKillFp = chkRequestKilled,
1586
             .chkKillParam = (void*)pRequest->self,
888,693,896✔
1587
             .pExecRes = NULL,
1588
             .source = pRequest->source,
888,723,058✔
1589
             .secureDelete = pRequest->secureDelete,
888,773,511✔
1590
             .pWorkerCb = getTaskPoolWorkerCb(),
888,694,552✔
1591
      };
1592

1593
      if (TSDB_CODE_SUCCESS == code) {
888,745,348✔
1594
        CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_EXECUTE);
1,777,713,027✔
1595
        code = schedulerExecJob(&req, &pRequest->body.queryJob);
888,839,496✔
1596
      }
1597
      taosArrayDestroy(pNodeList);
888,723,371✔
1598
      taosArrayDestroy(pMnodeList);
888,848,166✔
1599
      return code;
888,837,215✔
1600
    }
1601
  }
1602

1603
  qDestroyQueryPlan(pDag);
732,949✔
1604
  tscDebug("req:0x%" PRIx64 ", plan not executed, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
491,965✔
1605
           pRequest->requestId);
1606
  destorySqlCallbackWrapper(pWrapper);
491,965✔
1607
  pRequest->pWrapper = NULL;
491,965✔
1608
  if (TSDB_CODE_SUCCESS != code) {
491,965✔
1609
    pRequest->code = code;
282,591✔
1610
  }
1611

1612
  doRequestCallback(pRequest, code);
491,965✔
1613

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

1618
  return code;
489,000✔
1619
}
1620

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

1624
  if (pRequest->parseOnly) {
1,003,849,083✔
1625
    doRequestCallback(pRequest, 0);
325,046✔
1626
    return;
325,046✔
1627
  }
1628

1629
  pRequest->body.execMode = pQuery->execMode;
1,003,582,327✔
1630
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
1,003,551,238✔
1631
    destorySqlCallbackWrapper(pWrapper);
114,295,293✔
1632
    pRequest->pWrapper = NULL;
114,297,691✔
1633
  }
1634

1635
  if (pQuery->pRoot && !pRequest->inRetry) {
1,003,495,296✔
1636
    STscObj*            pTscObj = pRequest->pTscObj;
1,003,516,338✔
1637
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
1,003,557,141✔
1638
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
1,003,573,803✔
1639
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
677,783,034✔
1640
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
612,650,219✔
1641
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
390,934,313✔
1642
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
159,695,384✔
1643
    }
1644
  }
1645

1646
  switch (pQuery->execMode) {
1,003,706,835✔
1647
    case QUERY_EXEC_MODE_LOCAL:
6,750,875✔
1648
      asyncExecLocalCmd(pRequest, pQuery);
6,750,875✔
1649
      break;
6,750,875✔
1650
    case QUERY_EXEC_MODE_RPC:
106,891,869✔
1651
      code = asyncExecDdlQuery(pRequest, pQuery);
106,891,869✔
1652
      break;
106,884,915✔
1653
    case QUERY_EXEC_MODE_SCHEDULE: {
889,301,901✔
1654
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
889,301,901✔
1655
      break;
889,324,413✔
1656
    }
1657
    case QUERY_EXEC_MODE_EMPTY_RESULT:
652,065✔
1658
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
652,065✔
1659
      doRequestCallback(pRequest, 0);
652,065✔
1660
      break;
652,065✔
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) {
14,723✔
1669
  SCatalog* pCatalog = NULL;
14,723✔
1670
  int32_t   code = 0;
14,723✔
1671
  int32_t   dbNum = taosArrayGetSize(pRequest->dbList);
14,723✔
1672
  int32_t   tblNum = taosArrayGetSize(pRequest->tableList);
14,723✔
1673

1674
  if (dbNum <= 0 && tblNum <= 0) {
14,723✔
1675
    return TSDB_CODE_APP_ERROR;
13,931✔
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,371,070✔
1712
  SCatalog* pCatalog = NULL;
5,371,070✔
1713
  int32_t   tbNum = taosArrayGetSize(tbList);
5,371,070✔
1714
  int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
5,371,070✔
1715
  if (code != TSDB_CODE_SUCCESS) {
5,371,070✔
1716
    return code;
×
1717
  }
1718

1719
  if (isView) {
5,371,070✔
1720
    for (int32_t i = 0; i < tbNum; ++i) {
769,092✔
1721
      SName* pViewName = taosArrayGet(tbList, i);
384,546✔
1722
      char   dbFName[TSDB_DB_FNAME_LEN];
380,982✔
1723
      if (NULL == pViewName) {
384,546✔
1724
        continue;
×
1725
      }
1726
      (void)tNameGetFullDbName(pViewName, dbFName);
384,546✔
1727
      TSC_ERR_RET(catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0));
384,546✔
1728
    }
1729
  } else {
1730
    for (int32_t i = 0; i < tbNum; ++i) {
7,831,866✔
1731
      SName* pTbName = taosArrayGet(tbList, i);
2,845,342✔
1732
      TSC_ERR_RET(catalogRemoveTableMeta(pCatalog, pTbName));
2,845,342✔
1733
    }
1734
  }
1735

1736
  return TSDB_CODE_SUCCESS;
5,371,070✔
1737
}
1738

1739
int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) {
90,549,731✔
1740
  pEpSet->version = 0;
90,549,731✔
1741

1742
  // init mnode ip set
1743
  SEpSet* mgmtEpSet = &(pEpSet->epSet);
90,550,396✔
1744
  mgmtEpSet->numOfEps = 0;
90,550,408✔
1745
  mgmtEpSet->inUse = 0;
90,549,702✔
1746

1747
  if (firstEp && firstEp[0] != 0) {
90,549,403✔
1748
    if (strlen(firstEp) >= TSDB_EP_LEN) {
90,551,304✔
1749
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1750
      return -1;
×
1751
    }
1752

1753
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
90,551,304✔
1754
    if (code != TSDB_CODE_SUCCESS) {
90,552,431✔
1755
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1756
      return terrno;
×
1757
    }
1758
    // uint32_t addr = 0;
1759
    SIpAddr addr = {0};
90,552,431✔
1760
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
90,551,741✔
1761
    if (code) {
90,550,520✔
1762
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
142✔
1763
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1764
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
156✔
1765
    } else {
1766
      mgmtEpSet->numOfEps++;
90,551,792✔
1767
    }
1768
  }
1769

1770
  if (secondEp && secondEp[0] != 0) {
90,549,781✔
1771
    if (strlen(secondEp) >= TSDB_EP_LEN) {
2,074,844✔
1772
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1773
      return terrno;
×
1774
    }
1775

1776
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
2,074,844✔
1777
    if (code != TSDB_CODE_SUCCESS) {
2,074,812✔
1778
      return code;
×
1779
    }
1780
    SIpAddr addr = {0};
2,074,812✔
1781
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
2,074,825✔
1782
    if (code) {
2,074,383✔
1783
      tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
38✔
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,074,345✔
1788
    }
1789
  }
1790

1791
  if (mgmtEpSet->numOfEps == 0) {
90,549,743✔
1792
    terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
156✔
1793
    return TSDB_CODE_RPC_NETWORK_UNAVAIL;
156✔
1794
  }
1795

1796
  return 0;
90,548,913✔
1797
}
1798

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

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

1814
  pRequest->sqlstr = taosStrdup("taos_connect");
90,552,140✔
1815
  if (pRequest->sqlstr) {
90,550,766✔
1816
    pRequest->sqlLen = strlen(pRequest->sqlstr);
90,550,766✔
1817
  } else {
1818
    return terrno;
×
1819
  }
1820

1821
  SMsgSendInfo* body = NULL;
90,550,766✔
1822
  code = buildConnectMsg(pRequest, &body, totpCode);
90,550,766✔
1823
  if (TSDB_CODE_SUCCESS != code) {
90,548,775✔
1824
    destroyTscObj(*pTscObj);
×
1825
    return code;
×
1826
  }
1827

1828
  // int64_t transporterId = 0;
1829
  SEpSet epset = getEpSet_s(&(*pTscObj)->pAppInfo->mgmtEp);
90,548,775✔
1830
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &epset, NULL, body);
90,551,862✔
1831
  if (TSDB_CODE_SUCCESS != code) {
90,547,505✔
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)) {
90,547,505✔
1837
    destroyTscObj(*pTscObj);
34✔
1838
    tscError("failed to wait sem, code:%s", terrstr());
×
1839
    return terrno;
×
1840
  }
1841
  if (pRequest->code != TSDB_CODE_SUCCESS) {
90,550,346✔
1842
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
19,167✔
1843
    tscError("failed to connect to server, reason: %s", errorMsg);
19,167✔
1844

1845
    terrno = pRequest->code;
19,167✔
1846
    destroyRequest(pRequest);
19,167✔
1847
    taos_close_internal(*pTscObj);
19,167✔
1848
    *pTscObj = NULL;
19,167✔
1849
    return terrno;
19,167✔
1850
  }
1851
  if (connType == CONN_TYPE__AUTH_TEST) {
90,531,179✔
UNCOV
1852
    terrno = TSDB_CODE_SUCCESS;
×
UNCOV
1853
    destroyRequest(pRequest);
×
UNCOV
1854
    taos_close_internal(*pTscObj);
×
1855
    *pTscObj = NULL;
2,317✔
1856
    return TSDB_CODE_SUCCESS;
2,317✔
1857
  }
1858

1859
  tscInfo("conn:0x%" PRIx64 ", connection is opening, connId:%u, dnodeConn:%p, QID:0x%" PRIx64, (*pTscObj)->id,
90,531,179✔
1860
          (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId);
1861
  destroyRequest(pRequest);
90,533,975✔
1862
  return code;
90,529,620✔
1863
}
1864

1865
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo, int32_t totpCode) {
90,551,006✔
1866
  *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
90,551,006✔
1867
  if (*pMsgSendInfo == NULL) {
90,551,875✔
1868
    return terrno;
×
1869
  }
1870

1871
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
90,551,875✔
1872

1873
  (*pMsgSendInfo)->requestObjRefId = pRequest->self;
90,551,875✔
1874
  (*pMsgSendInfo)->requestId = pRequest->requestId;
90,551,875✔
1875
  (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType);
90,551,875✔
1876
  (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self));
90,551,557✔
1877
  if (NULL == (*pMsgSendInfo)->param) {
90,552,299✔
1878
    taosMemoryFree(*pMsgSendInfo);
×
1879
    return terrno;
×
1880
  }
1881

1882
  *(int64_t*)(*pMsgSendInfo)->param = pRequest->self;
90,552,299✔
1883

1884
  SConnectReq connectReq = {0};
90,552,299✔
1885
  STscObj*    pObj = pRequest->pTscObj;
90,552,299✔
1886

1887
  char* db = getDbOfConnection(pObj);
90,552,299✔
1888
  if (db != NULL) {
90,548,217✔
1889
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
459,449✔
1890
  } else if (terrno) {
90,088,768✔
1891
    taosMemoryFree(*pMsgSendInfo);
×
1892
    return terrno;
×
1893
  }
1894
  taosMemoryFreeClear(db);
90,552,517✔
1895

1896
  connectReq.connType = pObj->connType;
90,552,488✔
1897
  connectReq.pid = appInfo.pid;
90,552,488✔
1898
  connectReq.startTime = appInfo.startTime;
90,552,488✔
1899
  connectReq.totpCode = totpCode;
90,552,488✔
1900
  connectReq.connectTime = taosGetTimestampMs();
90,548,858✔
1901

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

1909
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
90,551,465✔
1910
  void*   pReq = taosMemoryMalloc(contLen);
90,547,159✔
1911
  if (NULL == pReq) {
90,551,639✔
1912
    taosMemoryFree(*pMsgSendInfo);
×
1913
    return terrno;
×
1914
  }
1915

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

1922
  (*pMsgSendInfo)->msgInfo.len = contLen;
90,546,142✔
1923
  (*pMsgSendInfo)->msgInfo.pData = pReq;
90,546,235✔
1924
  return TSDB_CODE_SUCCESS;
90,546,205✔
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,066,034✔
1933
    case TARGET_TYPE_MNODE:
676✔
1934
      if (NULL == pTscObj) {
676✔
1935
        tscError("mnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1936
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1937
        return;
1,188✔
1938
      }
1939

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

1955
      SCatalog* pCatalog = NULL;
3,827,219✔
1956
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
3,827,219✔
1957
      if (code != TSDB_CODE_SUCCESS) {
3,826,551✔
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);
3,826,551✔
1964
      if (code != TSDB_CODE_SUCCESS) {
3,827,424✔
1965
        tscError("fail to update catalog vg epset, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
12✔
1966
                 tstrerror(code));
1967
        return;
×
1968
      }
1969
      taosMemoryFreeClear(pSendInfo->target.dbFName);
3,827,412✔
1970
      break;
3,827,412✔
1971
    }
1972
    default:
241,141✔
1973
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
241,141✔
1974
      break;
241,717✔
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");
639,740✔
1982
    rpcFreeCont(pMsg->pCont);
639,740✔
1983
    taosMemoryFree(pEpSet);
639,740✔
1984
    return TSDB_CODE_TSC_INTERNAL_ERROR;
639,740✔
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,890,674,422✔
1998
    if (pRequest) {
1,890,662,202✔
1999
      if (pRequest->self != pSendInfo->requestObjRefId) {
1,875,788,690✔
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,875,788,907✔
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,875,738,100✔
2037
    if (TSDB_CODE_SUCCESS != code) {
1,875,818,317✔
2038
      tscError("doProcessMsgFromServer taosReleaseRef failed");
285✔
2039
      terrno = code;
285✔
2040
      pMsg->code = code;
285✔
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,070,736✔
2064
    if (NULL == tEpSet) {
4,070,667✔
2065
      code = terrno;
×
2066
      pMsg->code = terrno;
×
2067
      goto _exit;
×
2068
    }
2069
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
4,070,667✔
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) {
90,482,375✔
2076
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
2077
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
90,482,491✔
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;
372,511✔
2099
    taosMemoryFree(arg);
372,511✔
2100
    goto _exit;
307,813✔
2101
  }
2102
  return;
2,147,483,647✔
2103

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

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

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

2123
  STscObj* pObj = NULL;
2,429✔
2124
  int32_t  code = taos_connect_internal(ip, user, pass, totp, db, port, CONN_TYPE__QUERY, &pObj);
2,429✔
2125
  if (TSDB_CODE_SUCCESS == code) {
2,429✔
2126
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1,647✔
2127
    if (NULL == rid) {
1,647✔
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,647✔
2132
    return (TAOS*)rid;
1,647✔
2133
  } else {
2134
    terrno = code;
782✔
2135
  }
2136

2137
  return NULL;
782✔
2138
}
2139

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

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

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

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

2158
  STscObj* pObj = NULL;
2,889✔
2159
  int32_t  code = taos_connect_by_auth(ip, NULL, token, NULL, db, port, CONN_TYPE__QUERY, &pObj);
2,889✔
2160
  if (TSDB_CODE_SUCCESS == code) {
2,889✔
2161
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1,343✔
2162
    if (NULL == rid) {
1,343✔
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,343✔
2167
    return (TAOS*)rid;
1,343✔
2168
  } else {
2169
    terrno = code;
1,546✔
2170
  }
2171

2172
  return NULL;
1,546✔
2173
}
2174

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

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

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

2197
  return NULL;
139✔
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);
24,083✔
2213
          pResultInfo->row[i] = blobDataVal(pStart);
408✔
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;
296,510,160✔
2220
        pResultInfo->length[i] = 0;
296,621,351✔
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,276,003,152✔
2228
        pResultInfo->length[i] = 0;
1,276,506,906✔
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) {
238,405,116✔
2285
  tsem_t* sem = param;
238,405,116✔
2286
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
238,405,116✔
2287
    tscError("failed to post sem, code:%s", terrstr());
×
2288
  }
2289
}
238,405,165✔
2290

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

2296
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
1,801,299,830✔
2297
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
1,801,299,947✔
2298
    // All data has returned to App already, no need to try again
2299
    if (pResultInfo->completed) {
341,268,247✔
2300
      pResultInfo->numOfRows = 0;
102,863,188✔
2301
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
205,726,293✔
2302
      return NULL;
102,863,266✔
2303
    }
2304

2305
    // convert ucs4 to native multi-bytes string
2306
    pResultInfo->convertUcs4 = convertUcs4;
238,405,026✔
2307
    tsem_t sem;
237,700,683✔
2308
    if (TSDB_CODE_SUCCESS != tsem_init(&sem, 0, 0)) {
238,405,102✔
2309
      tscError("failed to init sem, code:%s", terrstr());
×
2310
    }
2311
    taos_fetch_rows_a(pRequest, syncFetchFn, &sem);
238,405,040✔
2312
    if (TSDB_CODE_SUCCESS != tsem_wait(&sem)) {
238,405,165✔
2313
      tscError("failed to wait sem, code:%s", terrstr());
×
2314
    }
2315
    if (TSDB_CODE_SUCCESS != tsem_destroy(&sem)) {
238,404,698✔
2316
      tscError("failed to destroy sem, code:%s", terrstr());
×
2317
    }
2318
    pRequest->inCallback = false;
238,404,197✔
2319
  }
2320

2321
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
1,698,436,849✔
2322
    return NULL;
12,162,828✔
2323
  } else {
2324
    if (setupOneRowPtr) {
1,686,274,005✔
2325
      doSetOneRowPtr(pResultInfo);
1,458,520,715✔
2326
      pResultInfo->current += 1;
1,458,520,705✔
2327
    }
2328

2329
    return pResultInfo->row;
1,686,273,995✔
2330
  }
2331
}
2332

2333
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
351,208,979✔
2334
  if (pResInfo->row == NULL) {
351,208,979✔
2335
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
223,560,654✔
2336
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
223,558,687✔
2337
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
223,548,152✔
2338
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
223,553,307✔
2339

2340
    if (pResInfo->row == NULL || pResInfo->pCol == NULL || pResInfo->length == NULL || pResInfo->convertBuf == NULL) {
223,556,344✔
2341
      taosMemoryFree(pResInfo->row);
9,523✔
2342
      taosMemoryFree(pResInfo->pCol);
×
2343
      taosMemoryFree(pResInfo->length);
×
2344
      taosMemoryFree(pResInfo->convertBuf);
×
2345
      return terrno;
×
2346
    }
2347
  }
2348

2349
  return TSDB_CODE_SUCCESS;
351,204,578✔
2350
}
2351

2352
static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength, bool isStmt) {
351,026,654✔
2353
  int32_t idx = -1;
351,026,654✔
2354
  iconv_t conv = taosAcquireConv(&idx, C2M, pResultInfo->charsetCxt);
351,026,654✔
2355
  if (conv == (iconv_t)-1) return TSDB_CODE_TSC_INTERNAL_ERROR;
351,014,102✔
2356

2357
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,034,220,817✔
2358
    int32_t type = pResultInfo->fields[i].type;
1,683,231,210✔
2359
    int32_t schemaBytes =
2360
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
1,683,227,253✔
2361

2362
    if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) {
1,683,221,043✔
2363
      char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]);
102,274,895✔
2364
      if (p == NULL) {
102,274,895✔
2365
        taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2366
        return terrno;
×
2367
      }
2368

2369
      pResultInfo->convertBuf[i] = p;
102,274,895✔
2370

2371
      SResultColumn* pCol = &pResultInfo->pCol[i];
102,274,895✔
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];
102,274,829✔
2393
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
102,274,829✔
2394
    }
2395
  }
2396
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
351,027,460✔
2397
  return TSDB_CODE_SUCCESS;
351,025,382✔
2398
}
2399

2400
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
351,018,059✔
2401
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,034,202,245✔
2402
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
1,683,208,625✔
2403
    TAOS_FIELD*   pField = pResultInfo->userFields + i;
1,683,202,875✔
2404
    int32_t       type = pFieldE->type;
1,683,198,860✔
2405
    int32_t       bufLen = 0;
1,683,201,899✔
2406
    char*         p = NULL;
1,683,201,899✔
2407
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
1,683,201,899✔
2408
      continue;
1,681,399,791✔
2409
    } else {
2410
      bufLen = 64;
1,791,715✔
2411
      p = taosMemoryRealloc(pResultInfo->convertBuf[i], bufLen * pResultInfo->numOfRows);
1,791,715✔
2412
      pFieldE->bytes = bufLen;
1,791,715✔
2413
      pField->bytes = bufLen;
1,791,715✔
2414
    }
2415
    if (!p) return terrno;
1,791,715✔
2416
    pResultInfo->convertBuf[i] = p;
1,791,715✔
2417

2418
    for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
1,084,738,183✔
2419
      int32_t code = decimalToStr((DecimalWord*)(pResultInfo->pCol[i].pData + j * tDataTypes[type].bytes), type,
1,082,946,468✔
2420
                                  pFieldE->precision, pFieldE->scale, p, bufLen);
1,082,946,468✔
2421
      p += bufLen;
1,082,946,468✔
2422
      if (TSDB_CODE_SUCCESS != code) {
1,082,946,468✔
2423
        return code;
×
2424
      }
2425
    }
2426
    pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
1,791,715✔
2427
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
1,791,715✔
2428
  }
2429
  return 0;
351,023,118✔
2430
}
2431

2432
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols) {
445,006✔
2433
  return sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) * 3 + sizeof(uint64_t) +
889,616✔
2434
         numOfCols * (sizeof(int8_t) + sizeof(int32_t));
444,610✔
2435
}
2436

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

2441
  int32_t numOfRows = pResultInfo->numOfRows;
222,503✔
2442
  int32_t numOfCols = pResultInfo->numOfCols;
222,503✔
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);
222,503✔
2447
  if (numOfCols != cols) {
222,503✔
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);
222,503✔
2453
  int32_t* colLength = (int32_t*)(p + len);
222,503✔
2454
  len += sizeof(int32_t) * numOfCols;
222,503✔
2455

2456
  char* pStart = p + len;
222,503✔
2457
  for (int32_t i = 0; i < numOfCols; ++i) {
948,212✔
2458
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
725,709✔
2459

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

2466
      int32_t estimateColLen = 0;
257,807✔
2467
      for (int32_t j = 0; j < numOfRows; ++j) {
1,236,709✔
2468
        if (offset[j] == -1) {
978,902✔
2469
          continue;
54,802✔
2470
        }
2471
        char* data = offset[j] + pStart;
924,100✔
2472

2473
        int32_t jsonInnerType = *data;
924,100✔
2474
        char*   jsonInnerData = data + CHAR_BYTES;
924,100✔
2475
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
924,100✔
2476
          estimateColLen += (VARSTR_HEADER_SIZE + strlen(TSDB_DATA_NULL_STR_L));
12,816✔
2477
        } else if (tTagIsJson(data)) {
911,284✔
2478
          estimateColLen += (VARSTR_HEADER_SIZE + ((const STag*)(data))->len);
226,731✔
2479
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
684,553✔
2480
          estimateColLen += varDataTLen(jsonInnerData) + CHAR_BYTES * 2;
636,493✔
2481
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
48,060✔
2482
          estimateColLen += (VARSTR_HEADER_SIZE + 32);
35,244✔
2483
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
12,816✔
2484
          estimateColLen += (VARSTR_HEADER_SIZE + 5);
12,816✔
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);
257,807✔
2493
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
467,902✔
2494
      int32_t lenTmp = numOfRows * sizeof(int32_t);
64,122✔
2495
      len += (lenTmp + colLen);
64,122✔
2496
      pStart += lenTmp;
64,122✔
2497
    } else {
2498
      int32_t lenTmp = BitmapLen(pResultInfo->numOfRows);
403,780✔
2499
      len += (lenTmp + colLen);
403,780✔
2500
      pStart += lenTmp;
403,780✔
2501
    }
2502
    pStart += colLen;
725,709✔
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);
222,503✔
2508
  return len;
222,503✔
2509
}
2510

2511
static int32_t doConvertJson(SReqResultInfo* pResultInfo) {
351,199,318✔
2512
  int32_t numOfRows = pResultInfo->numOfRows;
351,199,318✔
2513
  int32_t numOfCols = pResultInfo->numOfCols;
351,202,985✔
2514
  bool    needConvert = false;
351,202,533✔
2515
  for (int32_t i = 0; i < numOfCols; ++i) {
2,034,909,101✔
2516
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
1,683,924,095✔
2517
      needConvert = true;
222,503✔
2518
      break;
222,503✔
2519
    }
2520
  }
2521

2522
  if (!needConvert) {
351,207,509✔
2523
    return TSDB_CODE_SUCCESS;
350,985,006✔
2524
  }
2525

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

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

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

2541
  int32_t totalLen = 0;
222,503✔
2542
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
222,503✔
2543
  if (numOfCols != cols) {
222,503✔
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);
222,503✔
2549
  (void)memcpy(p1, p, len);
222,503✔
2550

2551
  p += len;
222,503✔
2552
  p1 += len;
222,503✔
2553
  totalLen += len;
222,503✔
2554

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

2563
  char* pStart = p;
222,503✔
2564
  char* pStart1 = p1;
222,503✔
2565
  for (int32_t i = 0; i < numOfCols; ++i) {
948,212✔
2566
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
725,709✔
2567
    int32_t colLen1 = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength1[i]) : colLength1[i];
725,709✔
2568
    if (colLen >= dataLen) {
725,709✔
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) {
725,709✔
2573
      int32_t* offset = (int32_t*)pStart;
257,807✔
2574
      int32_t* offset1 = (int32_t*)pStart1;
257,807✔
2575
      len = numOfRows * sizeof(int32_t);
257,807✔
2576
      (void)memcpy(pStart1, pStart, len);
257,807✔
2577
      pStart += len;
257,807✔
2578
      pStart1 += len;
257,807✔
2579
      totalLen += len;
257,807✔
2580

2581
      len = 0;
257,807✔
2582
      for (int32_t j = 0; j < numOfRows; ++j) {
1,236,709✔
2583
        if (offset[j] == -1) {
978,902✔
2584
          continue;
54,802✔
2585
        }
2586
        char* data = offset[j] + pStart;
924,100✔
2587

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

2631
        offset1[j] = len;
924,100✔
2632
        (void)memcpy(pStart1 + len, dst, varDataTLen(dst));
924,100✔
2633
        len += varDataTLen(dst);
924,100✔
2634
      }
2635
      colLen1 = len;
257,807✔
2636
      totalLen += colLen1;
257,807✔
2637
      colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len;
257,807✔
2638
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
467,902✔
2639
      len = numOfRows * sizeof(int32_t);
64,122✔
2640
      (void)memcpy(pStart1, pStart, len);
64,122✔
2641
      pStart += len;
64,122✔
2642
      pStart1 += len;
64,122✔
2643
      totalLen += len;
64,122✔
2644
      totalLen += colLen;
64,122✔
2645
      (void)memcpy(pStart1, pStart, colLen);
64,122✔
2646
    } else {
2647
      len = BitmapLen(pResultInfo->numOfRows);
403,780✔
2648
      (void)memcpy(pStart1, pStart, len);
403,780✔
2649
      pStart += len;
403,780✔
2650
      pStart1 += len;
403,780✔
2651
      totalLen += len;
403,780✔
2652
      totalLen += colLen;
403,780✔
2653
      (void)memcpy(pStart1, pStart, colLen);
403,780✔
2654
    }
2655
    pStart += colLen;
725,709✔
2656
    pStart1 += colLen1;
725,709✔
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);
222,503✔
2663

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

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

2676
  if (pResultInfo->numOfRows == 0) {
377,402,815✔
2677
    return TSDB_CODE_SUCCESS;
26,191,840✔
2678
  }
2679

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

2685
  int32_t code = doPrepareResPtr(pResultInfo);
351,207,671✔
2686
  if (code != TSDB_CODE_SUCCESS) {
351,204,626✔
2687
    return code;
×
2688
  }
2689
  code = doConvertJson(pResultInfo);
351,204,626✔
2690
  if (code != TSDB_CODE_SUCCESS) {
351,195,864✔
2691
    return code;
×
2692
  }
2693

2694
  char* p = (char*)pResultInfo->pData;
351,195,864✔
2695

2696
  // version:
2697
  int32_t blockVersion = *(int32_t*)p;
351,197,608✔
2698
  p += sizeof(int32_t);
351,201,568✔
2699

2700
  int32_t dataLen = *(int32_t*)p;
351,201,568✔
2701
  p += sizeof(int32_t);
351,201,972✔
2702

2703
  int32_t rows = *(int32_t*)p;
351,201,989✔
2704
  p += sizeof(int32_t);
351,202,745✔
2705

2706
  int32_t cols = *(int32_t*)p;
351,204,384✔
2707
  p += sizeof(int32_t);
351,203,396✔
2708

2709
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
351,205,528✔
2710
    tscError("setResultDataPtr paras error:rows;%d numOfRows:%" PRId64 " cols:%d numOfCols:%d", rows,
6,943✔
2711
             pResultInfo->numOfRows, cols, pResultInfo->numOfCols);
2712
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2713
  }
2714

2715
  int32_t hasColumnSeg = *(int32_t*)p;
351,199,925✔
2716
  p += sizeof(int32_t);
351,201,804✔
2717

2718
  uint64_t groupId = taosGetUInt64Aligned((uint64_t*)p);
351,206,141✔
2719
  p += sizeof(uint64_t);
351,206,141✔
2720

2721
  // check fields
2722
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,035,175,051✔
2723
    int8_t type = *(int8_t*)p;
1,683,987,129✔
2724
    p += sizeof(int8_t);
1,683,979,133✔
2725

2726
    int32_t bytes = *(int32_t*)p;
1,683,982,032✔
2727
    p += sizeof(int32_t);
1,683,982,542✔
2728

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

2734
  int32_t* colLength = (int32_t*)p;
351,210,503✔
2735
  p += sizeof(int32_t) * pResultInfo->numOfCols;
351,210,503✔
2736

2737
  char* pStart = p;
351,208,927✔
2738
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,035,232,430✔
2739
    if ((pStart - pResultInfo->pData) >= dataLen) {
1,684,021,556✔
2740
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2741
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2742
    }
2743
    if (blockVersion == BLOCK_VERSION_1) {
1,684,007,145✔
2744
      colLength[i] = htonl(colLength[i]);
1,211,313,970✔
2745
    }
2746
    if (colLength[i] >= dataLen) {
1,684,005,879✔
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,684,009,232✔
2751
      tscError("invalid type %d", pResultInfo->fields[i].type);
8,220✔
2752
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2753
    }
2754
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,684,014,633✔
2755
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
363,047,731✔
2756
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
363,034,825✔
2757
    } else {
2758
      pResultInfo->pCol[i].nullbitmap = pStart;
1,320,994,069✔
2759
      pStart += BitmapLen(pResultInfo->numOfRows);
1,320,996,859✔
2760
    }
2761

2762
    pResultInfo->pCol[i].pData = pStart;
1,684,021,446✔
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,684,031,503✔
2766

2767
    pStart += colLength[i];
1,684,027,739✔
2768
  }
2769

2770
  p = pStart;
351,213,901✔
2771
  // bool blankFill = *(bool*)p;
2772
  p += sizeof(bool);
351,213,901✔
2773
  int32_t offset = p - pResultInfo->pData;
351,213,901✔
2774
  if (offset > dataLen) {
351,212,157✔
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) {
351,212,157✔
2781
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
351,026,654✔
2782
  }
2783
#endif
2784
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
351,211,347✔
2785
    code = convertDecimalType(pResultInfo);
351,025,778✔
2786
  }
2787
  return code;
351,206,272✔
2788
}
2789

2790
char* getDbOfConnection(STscObj* pObj) {
1,287,682,636✔
2791
  terrno = TSDB_CODE_SUCCESS;
1,287,682,636✔
2792
  char* p = NULL;
1,287,705,367✔
2793
  (void)taosThreadMutexLock(&pObj->mutex);
1,287,705,367✔
2794
  size_t len = strlen(pObj->db);
1,287,743,394✔
2795
  if (len > 0) {
1,287,745,043✔
2796
    p = taosStrndup(pObj->db, tListLen(pObj->db));
901,695,655✔
2797
    if (p == NULL) {
901,692,849✔
2798
      tscError("failed to taosStrndup db name");
×
2799
    }
2800
  }
2801

2802
  (void)taosThreadMutexUnlock(&pObj->mutex);
1,287,742,237✔
2803
  return p;
1,287,687,904✔
2804
}
2805

2806
void setConnectionDB(STscObj* pTscObj, const char* db) {
90,336,431✔
2807
  if (db == NULL || pTscObj == NULL) {
90,336,431✔
2808
    tscError("setConnectionDB para is NULL");
50✔
2809
    return;
×
2810
  }
2811

2812
  (void)taosThreadMutexLock(&pTscObj->mutex);
90,385,109✔
2813
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
90,408,761✔
2814
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
90,408,731✔
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,
292,844,150✔
2828
                              bool isStmt) {
2829
  if (pResultInfo == NULL || pRsp == NULL) {
292,844,150✔
UNCOV
2830
    tscError("setQueryResultFromRsp paras is null");
×
2831
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2832
  }
2833

2834
  taosMemoryFreeClear(pResultInfo->pRspMsg);
292,844,186✔
2835
  pResultInfo->pRspMsg = (const char*)pRsp;
292,844,131✔
2836
  pResultInfo->numOfRows = htobe64(pRsp->numOfRows);
292,844,186✔
2837
  pResultInfo->current = 0;
292,844,186✔
2838
  pResultInfo->completed = (pRsp->completed == 1);
292,844,186✔
2839
  pResultInfo->precision = pRsp->precision;
292,844,150✔
2840

2841
  // decompress data if needed
2842
  int32_t payloadLen = htonl(pRsp->payloadLen);
292,844,173✔
2843

2844
  if (pRsp->compressed) {
292,844,137✔
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) {
292,844,137✔
2867
    int32_t compLen = *(int32_t*)pRsp->data;
266,653,265✔
2868
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
266,653,337✔
2869

2870
    char* pStart = (char*)pRsp->data + sizeof(int32_t) * 2;
266,653,281✔
2871

2872
    if (pRsp->compressed && compLen < rawLen) {
266,653,246✔
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;
266,653,222✔
2886
      pResultInfo->payloadLen = htonl(pRsp->compLen);
266,653,337✔
2887
      if (pRsp->compLen != pRsp->payloadLen) {
266,653,246✔
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;
292,844,124✔
2896

2897
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
292,844,137✔
2898
  return code;
292,842,887✔
2899
}
2900

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

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

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

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

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

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

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

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

2955
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
275✔
2956
  epSet.eps[0].port = (uint16_t)port;
275✔
2957
  int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
275✔
2958
  if (TSDB_CODE_SUCCESS != ret) {
275✔
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) {
275✔
2964
    tscError("failed to send server status req since %s", terrstr());
66✔
2965
    goto _OVER;
66✔
2966
  }
2967

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

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

2978
_OVER:
261✔
2979
  if (clientRpc != NULL) {
275✔
2980
    rpcClose(clientRpc);
275✔
2981
  }
2982
  if (rpcRsp.pCont != NULL) {
275✔
2983
    rpcFreeCont(rpcRsp.pCont);
209✔
2984
  }
2985
  return code;
275✔
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,097,403,334✔
3196
  SSyncQueryParam* pParam = param;
1,097,403,334✔
3197
  pParam->pRequest = res;
1,097,403,334✔
3198

3199
  if (pParam->pRequest) {
1,097,417,755✔
3200
    pParam->pRequest->code = code;
1,097,381,913✔
3201
    clientOperateReport(pParam->pRequest);
1,097,399,094✔
3202
  }
3203

3204
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
1,097,273,691✔
3205
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
3206
  }
3207
}
1,097,475,115✔
3208

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

UNCOV
3217
    return;
×
3218
  }
3219

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

3228
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, sql:%s", connId, sql);
1,096,980,890✔
3229

3230
  SRequestObj* pRequest = NULL;
1,096,981,225✔
3231
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, 0);
1,097,005,867✔
3232
  if (code != TSDB_CODE_SUCCESS) {
1,096,987,994✔
3233
    terrno = code;
3,070✔
3234
    fp(param, NULL, terrno);
3,070✔
3235
    return;
3,070✔
3236
  }
3237

3238
  code = connCheckAndUpateMetric(connId);
1,096,984,924✔
3239
  if (code != TSDB_CODE_SUCCESS) {
1,096,977,105✔
3240
    terrno = code;
356✔
3241
    fp(param, NULL, terrno);
356✔
3242
    return;
356✔
3243
  }
3244

3245
  pRequest->source = source;
1,096,976,749✔
3246
  pRequest->body.queryFp = fp;
1,096,970,738✔
3247
  doAsyncQuery(pRequest, false);
1,096,985,853✔
3248
}
3249

3250
void taosAsyncQueryImplWithReqid(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
205✔
3251
                                 int64_t reqid) {
3252
  if (sql == NULL || NULL == fp) {
205✔
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);
205✔
3262
  if (sqlLen > (size_t)tsMaxSQLLength) {
205✔
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);
205✔
3270

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

3279
  code = connCheckAndUpateMetric(connId);
199✔
3280

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

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

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

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

3298
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
1,096,858,268✔
3299
  if (NULL == param) {
1,096,874,843✔
3300
    return NULL;
×
3301
  }
3302

3303
  int32_t code = tsem_init(&param->sem, 0, 0);
1,096,874,843✔
3304
  if (TSDB_CODE_SUCCESS != code) {
1,096,849,220✔
3305
    taosMemoryFree(param);
×
3306
    return NULL;
×
3307
  }
3308

3309
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
1,096,849,220✔
3310
  code = tsem_wait(&param->sem);
1,096,794,115✔
3311
  if (TSDB_CODE_SUCCESS != code) {
1,096,876,066✔
3312
    taosMemoryFree(param);
×
3313
    return NULL;
×
3314
  }
3315
  code = tsem_destroy(&param->sem);
1,096,876,066✔
3316
  if (TSDB_CODE_SUCCESS != code) {
1,096,865,041✔
3317
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3318
  }
3319

3320
  SRequestObj* pRequest = NULL;
1,096,880,364✔
3321
  if (param->pRequest != NULL) {
1,096,880,364✔
3322
    param->pRequest->syncQuery = true;
1,096,874,413✔
3323
    pRequest = param->pRequest;
1,096,877,156✔
3324
    param->pRequest->inCallback = false;
1,096,875,481✔
3325
  }
3326
  taosMemoryFree(param);
1,096,873,818✔
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,096,882,950✔
3332
}
3333

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

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

3350
  taosAsyncQueryImplWithReqid(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, reqid);
205✔
3351
  code = tsem_wait(&param->sem);
205✔
3352
  if (TSDB_CODE_SUCCESS != code) {
205✔
3353
    taosMemoryFree(param);
×
3354
    return NULL;
×
3355
  }
3356
  SRequestObj* pRequest = NULL;
205✔
3357
  if (param->pRequest != NULL) {
205✔
3358
    param->pRequest->syncQuery = true;
199✔
3359
    pRequest = param->pRequest;
199✔
3360
  }
3361
  taosMemoryFree(param);
205✔
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;
205✔
3367
}
3368

3369
static void fetchCallback(void* pResult, void* param, int32_t code) {
289,016,441✔
3370
  SRequestObj* pRequest = (SRequestObj*)param;
289,016,441✔
3371

3372
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
289,016,441✔
3373

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

3377
  pResultInfo->pData = pResult;
289,016,441✔
3378
  pResultInfo->numOfRows = 0;
289,016,441✔
3379

3380
  if (code != TSDB_CODE_SUCCESS) {
289,016,333✔
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) {
289,016,333✔
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,
293,869,836✔
3394
                                         pResultInfo->convertUcs4, pRequest->stmtBindVersion > 0);
289,016,405✔
3395
  if (pRequest->code != TSDB_CODE_SUCCESS) {
289,015,188✔
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(
289,014,691✔
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;
289,015,714✔
3405
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
289,016,362✔
3406
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
289,016,254✔
3407
  }
3408

3409
  pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows);
289,016,325✔
3410
}
3411

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

3416
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
325,760,381✔
3417

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

3424
    return;
2,039,517✔
3425
  }
3426

3427
  // all data has returned to App already, no need to try again
3428
  if (pResultInfo->completed) {
325,760,373✔
3429
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
73,487,880✔
3430
    // it is a local executed query, no need to do async fetch
3431
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
36,743,940✔
3432
      if (pResultInfo->localResultFetched) {
1,696,680✔
3433
        pResultInfo->numOfRows = 0;
848,340✔
3434
        pResultInfo->current = 0;
848,340✔
3435
      } else {
3436
        pResultInfo->localResultFetched = true;
848,340✔
3437
      }
3438
    } else {
3439
      pResultInfo->numOfRows = 0;
35,047,260✔
3440
    }
3441

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

3446
  SSchedulerReq req = {
289,016,441✔
3447
      .syncReq = false,
3448
      .fetchFp = fetchCallback,
3449
      .cbParam = pRequest,
3450
  };
3451

3452
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
289,016,441✔
3453
  if (TSDB_CODE_SUCCESS != code) {
289,016,441✔
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,097,392,667✔
3460
  pRequest->inCallback = true;
1,097,392,667✔
3461

3462
  int64_t this = pRequest->self;
1,097,429,383✔
3463
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
1,097,338,871✔
3464
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
26,572✔
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) {
×
UNCOV
3468
      pRequest->code = TSDB_CODE_SUCCESS;
×
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,097,389,264✔
3476
           pRequest);
3477

3478
  if (pRequest->body.queryFp != NULL) {
1,097,389,671✔
3479
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
1,097,403,073✔
3480
  }
3481

3482
  SRequestObj* pReq = acquireRequest(this);
1,097,478,183✔
3483
  if (pReq != NULL) {
1,097,513,739✔
3484
    pReq->inCallback = false;
1,095,119,945✔
3485
    (void)releaseRequest(this);
1,095,119,703✔
3486
  }
3487
}
1,097,475,192✔
3488

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

3498
void updateConnAccessInfo(SConnAccessInfo* pInfo) {
1,187,483,171✔
3499
  if (pInfo == NULL) {
1,187,483,171✔
3500
    return;
×
3501
  }
3502
  int64_t ts = taosGetTimestampMs();
1,187,537,445✔
3503
  if (pInfo->startTime == 0) {
1,187,537,445✔
3504
    pInfo->startTime = ts;
90,552,264✔
3505
  }
3506
  pInfo->lastAccessTime = ts;
1,187,533,410✔
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