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

taosdata / TDengine / #5053

13 May 2026 12:00PM UTC coverage: 73.397% (+0.06%) from 73.338%
#5053

push

travis-ci

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

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

627 existing lines in 131 files now uncovered.

281694 of 383795 relevant lines covered (73.4%)

132505311.38 hits per line

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

73.75
/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) {
763,654,704✔
40
  SRequestObj* pReq = acquireRequest(rId);
763,654,704✔
41
  if (pReq != NULL) {
763,694,755✔
42
    pReq->isQuery = true;
763,665,856✔
43
    (void)releaseRequest(rId);
763,664,249✔
44
  }
45
}
763,680,299✔
46

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

52
  size_t len = strlen(str);
202,505,659✔
53
  if (len <= 0 || len > maxsize) {
202,505,659✔
UNCOV
54
    return false;
×
55
  }
56

57
  return true;
202,506,020✔
58
}
59

60
static bool validateUserName(const char* user) { return stringLengthCheck(user, TSDB_USER_LEN - 1); }
101,007,278✔
61

62
static bool validatePassword(const char* passwd) { return stringLengthCheck(passwd, TSDB_PASSWORD_MAX_LEN); }
101,006,414✔
63

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

66
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
101,007,897✔
67
  char key[512] = {0};
101,007,897✔
68
  if (user == NULL) {
101,008,540✔
69
    (void)snprintf(key, sizeof(key), "%s:%s:%d", auth, ip, port);
2,959✔
70
  } else {
71
    (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
101,005,581✔
72
  }
73
  return taosStrdup(key);
101,008,540✔
74
}
75

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

81
  size_t escapeLength = 0;
661,428✔
82
  for (size_t i = 0; i < srcLength; ++i) {
18,748,092✔
83
    if (src[i] == '\"' || src[i] == '\\' || src[i] == '\b' || src[i] == '\f' || src[i] == '\n' || src[i] == '\r' ||
18,086,664✔
84
        src[i] == '\t') {
18,086,664✔
85
      escapeLength += 1;
×
86
    }
87
  }
88

89
  size_t dstLength = srcLength;
661,428✔
90
  if (escapeLength == 0) {
661,428✔
91
    (void)memcpy(dst, src, srcLength);
661,428✔
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;
661,428✔
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;
547✔
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,671,514✔
146
  taosHashCleanup(appInfo.pInstMap);
1,671,514✔
147
  taosHashCleanup(appInfo.pInstMapByClusterId);
1,671,514✔
148
  tscInfo("cluster instance map cleaned");
1,671,514✔
149
}
1,671,514✔
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,
101,007,516✔
156
                             uint16_t port, int connType, STscObj** pObj) {
157
  TSC_ERR_RET(taos_init());
101,007,516✔
158

159
  if (user == NULL) {
101,010,021✔
160
    if (auth == NULL || strlen(auth) != (TSDB_TOKEN_LEN - 1)) {
3,969✔
161
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_TOKEN);
1,010✔
162
    }
163
  } else if (!validateUserName(user)) {
101,006,052✔
164
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
165
  }
166
  int32_t code = 0;
101,009,944✔
167

168
  char localDb[TSDB_DB_NAME_LEN] = {0};
101,009,944✔
169
  if (db != NULL && strlen(db) > 0) {
101,009,944✔
170
    if (!validateDbName(db)) {
490,312✔
171
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
172
    }
173

174
    tstrncpy(localDb, db, sizeof(localDb));
490,285✔
175
    (void)strdequote(localDb);
490,257✔
176
  }
177

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

187
  SCorEpSet epSet = {0};
101,008,189✔
188
  if (ip) {
101,008,185✔
189
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
98,822,089✔
190
  } else {
191
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
2,186,096✔
192
  }
193

194
  if (port) {
101,007,949✔
195
    epSet.epSet.eps[0].port = port;
97,929,395✔
196
    epSet.epSet.eps[1].port = port;
97,929,395✔
197
  }
198

199
  char* key = getClusterKey(user, auth, ip, port);
101,007,949✔
200
  if (NULL == key) {
101,010,288✔
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,
101,010,288✔
204
          user ? user : "", db, key);
205
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
204,206,919✔
206
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
103,196,856✔
207
  }
208

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

249
    pInst = &p;
1,776,620✔
250
  } else {
251
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
99,233,851✔
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);
99,233,851✔
257
  }
258

259
_return:
101,010,697✔
260

261
  if (TSDB_CODE_SUCCESS != code) {
101,010,697✔
262
    (void)taosThreadMutexUnlock(&appInfo.mutex);
226✔
263
    taosMemoryFreeClear(key);
226✔
264
    return code;
226✔
265
  } else {
266
    code = taosThreadMutexUnlock(&appInfo.mutex);
101,010,471✔
267
    taosMemoryFreeClear(key);
101,009,184✔
268
    if (TSDB_CODE_SUCCESS != code) {
101,010,471✔
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);
101,010,471✔
273
  }
274
}
275

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

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

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

312
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
1,157,101,872✔
313
  if ((*pRequest)->sqlstr == NULL) {
1,157,109,785✔
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,157,105,724✔
321
  (*pRequest)->sqlstr[sqlLen] = 0;
1,157,162,302✔
322
  (*pRequest)->sqlLen = sqlLen;
1,157,167,050✔
323
  (*pRequest)->validateOnly = validateSql;
1,157,172,975✔
324
  (*pRequest)->stmtBindVersion = 0;
1,157,167,395✔
325

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

335
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
1,157,077,773✔
336

337
  STscObj* pTscObj = (*pRequest)->pTscObj;
1,157,126,084✔
338
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
1,157,112,863✔
339
                             sizeof((*pRequest)->self));
340
  if (err) {
1,157,114,211✔
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,157,114,211✔
349
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
1,157,123,390✔
350
    if (TSDB_CODE_SUCCESS !=
515,503,889✔
351
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
515,500,289✔
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,157,231,664✔
361
  return TSDB_CODE_SUCCESS;
1,157,102,196✔
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,892,972✔
377
  STscObj* pTscObj = pRequest->pTscObj;
7,892,972✔
378

379
  SParseContext cxt = {
7,896,017✔
380
      .requestId = pRequest->requestId,
7,894,892✔
381
      .requestRid = pRequest->self,
7,892,231✔
382
      .acctId = pTscObj->acctId,
7,893,667✔
383
      .db = pRequest->pDb,
7,892,095✔
384
      .topicQuery = topicQuery,
385
      .pSql = pRequest->sqlstr,
7,895,252✔
386
      .sqlLen = pRequest->sqlLen,
7,896,064✔
387
      .pMsg = pRequest->msgBuf,
7,896,101✔
388
      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
389
      .pTransporter = pTscObj->pAppInfo->pTransporter,
7,894,814✔
390
      .pStmtCb = pStmtCb,
391
      .pUser = pTscObj->user,
7,894,589✔
392
      .userId = pTscObj->userId,
7,890,546✔
393
      .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
7,893,737✔
394
      .enableSysInfo = pTscObj->sysInfo,
7,893,356✔
395
      .minSecLevel = pTscObj->minSecLevel,
7,892,545✔
396
      .maxSecLevel = pTscObj->maxSecLevel,
7,892,028✔
397
      .macMode = pTscObj->pAppInfo->serverCfg.macActive,  // propagates cluster-level MAC state into parser/executor
7,891,781✔
398
      .sodInitial = pTscObj->pAppInfo->serverCfg.sodInitial,
7,892,567✔
399
      .svrVer = pTscObj->sVer,
7,895,287✔
400
      .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
7,895,756✔
401
      .stmtBindVersion = pRequest->stmtBindVersion,
7,896,454✔
402
      .setQueryFp = setQueryRequest,
403
      .timezone = pTscObj->optionInfo.timezone,
7,894,750✔
404
      .charsetCxt = pTscObj->optionInfo.charsetCxt,
7,893,282✔
405
  };
406

407
  cxt.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
7,892,944✔
408
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.pCatalog);
7,899,146✔
409
  if (code != TSDB_CODE_SUCCESS) {
7,893,307✔
410
    return code;
×
411
  }
412

413
  code = qParseSql(&cxt, pQuery);
7,893,307✔
414
  if (TSDB_CODE_SUCCESS == code) {
7,885,932✔
415
    if ((*pQuery)->haveResultSet) {
7,884,614✔
416
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols,
455✔
417
                              (*pQuery)->pResExtSchema, pRequest->stmtBindVersion > 0);
455✔
418
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
455✔
419
    }
420
  }
421

422
  if (TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) {
7,886,722✔
423
    TSWAP(pRequest->dbList, (*pQuery)->pDbList);
7,881,277✔
424
    TSWAP(pRequest->tableList, (*pQuery)->pTableList);
7,884,448✔
425
    TSWAP(pRequest->targetTableList, (*pQuery)->pTargetTableList);
7,884,484✔
426
  }
427

428
  taosArrayDestroy(cxt.pTableMetaPos);
7,881,162✔
429
  taosArrayDestroy(cxt.pTableVgroupPos);
7,881,976✔
430

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

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

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

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

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

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

498
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
1,917,068,466✔
499

500
void asyncExecLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
7,030,589✔
501
  SRetrieveTableRsp* pRsp = NULL;
7,030,589✔
502
  if (pRequest->validateOnly) {
7,030,590✔
503
    doRequestCallback(pRequest, 0);
12,015✔
504
    return;
12,015✔
505
  }
506

507
  uint8_t showVarPrivMask = SHOW_VAR_PRIV_ALL;
7,018,574✔
508
#ifdef TD_ENTERPRISE
509
  if (pQuery->pRoot != NULL && nodeType(pQuery->pRoot) == QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT) {
7,018,574✔
510
    showVarPrivMask = getShowVarPrivMask(pRequest);
19,997✔
511
  }
512
#endif
513
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, showVarPrivMask, pQuery->pRoot, &pRsp,
13,998,918✔
514
                              atomic_load_8(&pRequest->pTscObj->biMode), pRequest->pTscObj->optionInfo.charsetCxt);
13,998,901✔
515
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
7,018,575✔
516
    code = setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4,
3,890,110✔
517
                                 pRequest->stmtBindVersion > 0);
3,890,110✔
518
  }
519

520
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
7,018,535✔
521
  pRequest->code = code;
7,018,535✔
522

523
  if (pRequest->code != TSDB_CODE_SUCCESS) {
7,018,551✔
524
    pResultInfo->numOfRows = 0;
2,388✔
525
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
2,388✔
526
             pRequest->requestId);
527
  } else {
528
    tscDebug(
7,016,122✔
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);
7,018,510✔
534
}
535

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

542
  // drop table if exists not_exists_table
543
  if (NULL == pQuery->pCmdMsg) {
117,833,518✔
544
    doRequestCallback(pRequest, 0);
8,157✔
545
    return TSDB_CODE_SUCCESS;
8,157✔
546
  }
547

548
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
117,825,491✔
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;
117,825,110✔
552
  pRequest->type = pMsgInfo->msgType;
117,825,227✔
553
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
117,825,481✔
554
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
117,825,218✔
555

556
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
117,824,833✔
557
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
117,824,417✔
558

559
  int32_t code = asyncSendMsgToServer(pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg);
117,831,252✔
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);
117,833,364✔
565
  if (code) {
117,835,378✔
566
    doRequestCallback(pRequest, code);
×
567
  }
568
  return code;
117,836,312✔
569
}
570

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

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

579
  return node1->load > node2->load;
209,772✔
580
}
581

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

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

598
  return TSDB_CODE_SUCCESS;
413,912✔
599
}
600

601
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
1,150,550,663✔
602
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
1,150,550,663✔
603
    *required = false;
1,136,619,865✔
604
    return TSDB_CODE_SUCCESS;
1,136,612,492✔
605
  }
606

607
  int32_t       code = TSDB_CODE_SUCCESS;
13,930,798✔
608
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
13,930,798✔
609
  *required = false;
13,930,228✔
610

611
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
13,930,798✔
612
  *required = (NULL == pInfo->pQnodeList);
13,930,798✔
613
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
13,930,798✔
614
  return TSDB_CODE_SUCCESS;
13,930,798✔
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,485,213✔
650
  pRequest->type = pQuery->msgType;
9,485,213✔
651
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
9,485,106✔
652

653
  SPlanContext cxt = {.queryId = pRequest->requestId,
9,904,467✔
654
                      .acctId = pRequest->pTscObj->acctId,
9,486,193✔
655
                      .mgmtEpSet = getEpSet_s(&pAppInfo->mgmtEp),
9,481,659✔
656
                      .pAstRoot = pQuery->pRoot,
9,493,386✔
657
                      .showRewrite = pQuery->showRewrite,
9,491,467✔
658
                      .pMsg = pRequest->msgBuf,
9,489,922✔
659
                      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
660
                      .pUser = pRequest->pTscObj->user,
9,484,972✔
661
                      .userId = pRequest->pTscObj->userId,
9,478,694✔
662
                      .timezone = pRequest->pTscObj->optionInfo.timezone,
9,489,556✔
663
                      .sysInfo = pRequest->pTscObj->sysInfo,
9,483,352✔
664
                      .minSecLevel = pRequest->pTscObj->minSecLevel,
9,488,481✔
665
                      .maxSecLevel = pRequest->pTscObj->maxSecLevel,
9,482,895✔
666
                      .macMode = pAppInfo->serverCfg.macActive};
9,484,083✔
667

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

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

678
  pResInfo->numOfCols = numOfCols;
308,753,415✔
679
  if (pResInfo->fields != NULL) {
308,752,672✔
680
    taosMemoryFree(pResInfo->fields);
14,938✔
681
  }
682
  if (pResInfo->userFields != NULL) {
308,747,426✔
683
    taosMemoryFree(pResInfo->userFields);
14,938✔
684
  }
685
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
308,751,603✔
686
  if (NULL == pResInfo->fields) return terrno;
308,740,771✔
687
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
308,744,485✔
688
  if (NULL == pResInfo->userFields) {
308,730,702✔
689
    taosMemoryFree(pResInfo->fields);
×
690
    return terrno;
×
691
  }
692
  if (numOfCols != pResInfo->numOfCols) {
308,732,091✔
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,578,295,012✔
698
    pResInfo->fields[i].type = pSchema[i].type;
1,269,544,035✔
699

700
    pResInfo->userFields[i].type = pSchema[i].type;
1,269,549,508✔
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,269,552,158✔
703
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
1,269,547,100✔
704
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
1,269,550,122✔
705
      decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale);
1,638,729✔
706
    }
707

708
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
1,269,561,905✔
709
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
1,269,571,368✔
710
  }
711
  return TSDB_CODE_SUCCESS;
308,759,557✔
712
}
713

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

720
  pResInfo->precision = precision;
223,442,933✔
721
}
722

723
int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) {
228,328,269✔
724
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
228,328,269✔
725
  if (NULL == nodeList) {
228,348,542✔
726
    return terrno;
×
727
  }
728
  char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client";
228,353,489✔
729

730
  int32_t dbNum = taosArrayGetSize(pDbVgList);
228,353,489✔
731
  for (int32_t i = 0; i < dbNum; ++i) {
453,805,503✔
732
    SArray* pVg = taosArrayGetP(pDbVgList, i);
225,418,613✔
733
    if (NULL == pVg) {
225,433,166✔
734
      continue;
×
735
    }
736
    int32_t vgNum = taosArrayGetSize(pVg);
225,433,166✔
737
    if (vgNum <= 0) {
225,431,701✔
738
      continue;
701,156✔
739
    }
740

741
    for (int32_t j = 0; j < vgNum; ++j) {
766,552,330✔
742
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
541,810,746✔
743
      if (NULL == pInfo) {
541,828,297✔
744
        taosArrayDestroy(nodeList);
×
745
        return TSDB_CODE_OUT_OF_RANGE;
×
746
      }
747
      SQueryNodeLoad load = {0};
541,828,297✔
748
      load.addr.nodeId = pInfo->vgId;
541,816,161✔
749
      load.addr.epSet = pInfo->epSet;
541,825,308✔
750

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

758
  int32_t vnodeNum = taosArrayGetSize(nodeList);
228,386,890✔
759
  if (vnodeNum > 0) {
228,373,798✔
760
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
224,365,086✔
761
    goto _return;
224,363,344✔
762
  }
763

764
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
4,008,712✔
765
  if (mnodeNum <= 0) {
4,003,592✔
766
    tscDebug("0x%" PRIx64 " %s policy, empty node list", pRequest->requestId, policy);
×
767
    goto _return;
×
768
  }
769

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

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

782
_return:
152,594✔
783

784
  *pNodeList = nodeList;
228,363,207✔
785

786
  return TSDB_CODE_SUCCESS;
228,353,967✔
787
}
788

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

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

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

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

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

828
_return:
×
829

830
  *pNodeList = nodeList;
1,767,290✔
831

832
  return TSDB_CODE_SUCCESS;
1,767,290✔
833
}
834

835
void freeVgList(void* list) {
9,345,159✔
836
  SArray* pList = *(SArray**)list;
9,345,159✔
837
  taosArrayDestroy(pList);
9,347,544✔
838
}
9,345,022✔
839

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

846
  switch (tsQueryPolicy) {
220,615,973✔
847
    case QUERY_POLICY_VNODE:
218,861,612✔
848
    case QUERY_POLICY_CLIENT: {
849
      if (pResultMeta) {
218,861,612✔
850
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
218,869,176✔
851
        if (NULL == pDbVgList) {
218,869,085✔
852
          code = terrno;
×
853
          goto _return;
×
854
        }
855
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
218,869,085✔
856
        for (int32_t i = 0; i < dbNum; ++i) {
434,933,219✔
857
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
216,065,509✔
858
          if (pRes->code || NULL == pRes->pRes) {
216,066,061✔
859
            continue;
677✔
860
          }
861

862
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
432,144,510✔
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);
218,869,492✔
907
      break;
218,863,881✔
908
    }
909
    case QUERY_POLICY_HYBRID:
1,767,290✔
910
    case QUERY_POLICY_QNODE: {
911
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
1,808,050✔
912
        SMetaRes* pRes = taosArrayGet(pResultMeta->pQnodeList, 0);
40,760✔
913
        if (pRes->code) {
40,760✔
914
          pQnodeList = NULL;
×
915
        } else {
916
          pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL);
40,760✔
917
          if (NULL == pQnodeList) {
40,760✔
918
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
919
            goto _return;
×
920
          }
921
        }
922
      } else {
923
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
1,726,530✔
924
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
1,726,530✔
925
        if (pInst->pQnodeList) {
1,726,530✔
926
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
1,726,530✔
927
          if (NULL == pQnodeList) {
1,726,530✔
928
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
929
            goto _return;
×
930
          }
931
        }
932
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
1,726,530✔
933
      }
934

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

943
_return:
220,631,171✔
944
  taosArrayDestroyEx(pDbVgList, fp);
220,631,171✔
945
  taosArrayDestroy(pQnodeList);
220,634,665✔
946

947
  return code;
220,637,589✔
948
}
949

950
int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList) {
9,478,240✔
951
  SArray* pDbVgList = NULL;
9,478,240✔
952
  SArray* pQnodeList = NULL;
9,478,240✔
953
  int32_t code = 0;
9,481,064✔
954

955
  switch (tsQueryPolicy) {
9,481,064✔
956
    case QUERY_POLICY_VNODE:
9,468,450✔
957
    case QUERY_POLICY_CLIENT: {
958
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
9,468,450✔
959
      if (dbNum > 0) {
9,489,516✔
960
        SCatalog*     pCtg = NULL;
9,350,337✔
961
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
9,342,021✔
962
        code = catalogGetHandle(pInst->clusterId, &pCtg);
9,345,643✔
963
        if (code != TSDB_CODE_SUCCESS) {
9,347,302✔
964
          goto _return;
×
965
        }
966

967
        pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
9,347,302✔
968
        if (NULL == pDbVgList) {
9,347,847✔
969
          code = terrno;
×
970
          goto _return;
×
971
        }
972
        SArray* pVgList = NULL;
9,347,916✔
973
        for (int32_t i = 0; i < dbNum; ++i) {
18,692,473✔
974
          char*            dbFName = taosArrayGet(pRequest->dbList, i);
9,344,535✔
975
          SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
9,348,244✔
976
                                   .requestId = pRequest->requestId,
9,348,697✔
977
                                   .requestObjRefId = pRequest->self,
9,346,142✔
978
                                   .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
9,344,295✔
979

980
          // catalogGetDBVgList will handle dbFName == null.
981
          code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
9,357,064✔
982
          if (code) {
9,345,939✔
983
            goto _return;
×
984
          }
985

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

993
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
9,495,827✔
994
      break;
9,485,265✔
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:
12,614✔
1004
      tscError("unknown query policy: %d", tsQueryPolicy);
12,614✔
1005
      return TSDB_CODE_APP_ERROR;
×
1006
  }
1007

1008
_return:
9,484,248✔
1009

1010
  taosArrayDestroyEx(pDbVgList, freeVgList);
9,484,709✔
1011
  taosArrayDestroy(pQnodeList);
9,480,631✔
1012

1013
  return code;
9,488,265✔
1014
}
1015

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

1019
  SExecResult      res = {0};
9,493,732✔
1020
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
9,490,985✔
1021
                           .requestId = pRequest->requestId,
9,489,277✔
1022
                           .requestObjRefId = pRequest->self};
9,482,374✔
1023
  SSchedulerReq    req = {
9,894,501✔
1024
         .syncReq = true,
1025
         .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
9,476,239✔
1026
         .pConn = &conn,
1027
         .pNodeList = pNodeList,
1028
         .pDag = pDag,
1029
         .sql = pRequest->sqlstr,
9,476,239✔
1030
         .startTs = pRequest->metric.start,
9,485,985✔
1031
         .execFp = NULL,
1032
         .cbParam = NULL,
1033
         .chkKillFp = chkRequestKilled,
1034
         .chkKillParam = (void*)pRequest->self,
9,479,910✔
1035
         .pExecRes = &res,
1036
         .source = pRequest->source,
9,480,122✔
1037
         .secureDelete = pRequest->secureDelete,
9,478,085✔
1038
         .pWorkerCb = getTaskPoolWorkerCb(),
9,487,043✔
1039
  };
1040

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

1043
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
9,495,475✔
1044
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
9,495,522✔
1045

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

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

1054
  if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_DELETE == pRequest->type ||
9,493,718✔
1055
      TDMT_VND_CREATE_TABLE == pRequest->type) {
104,509✔
1056
    pRequest->body.resInfo.numOfRows = res.numOfRows;
9,452,481✔
1057
    if (TDMT_VND_SUBMIT == pRequest->type) {
9,453,381✔
1058
      STscObj*            pTscObj = pRequest->pTscObj;
9,386,624✔
1059
      SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
9,387,483✔
1060
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows);
9,391,340✔
1061
    }
1062

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

1066
  pRequest->code = res.code;
9,494,093✔
1067
  terrno = res.code;
9,496,126✔
1068
  return pRequest->code;
9,491,215✔
1069
}
1070

1071
int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
641,651,949✔
1072
  SArray*      pArray = NULL;
641,651,949✔
1073
  SSubmitRsp2* pRsp = (SSubmitRsp2*)res;
641,651,949✔
1074
  if (NULL == pRsp->aCreateTbRsp) {
641,651,949✔
1075
    return TSDB_CODE_SUCCESS;
627,135,977✔
1076
  }
1077

1078
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
14,539,663✔
1079
  for (int32_t i = 0; i < tbNum; ++i) {
32,473,043✔
1080
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
17,933,622✔
1081
    if (pTbRsp->pMeta) {
17,935,462✔
1082
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
17,576,748✔
1083
    }
1084
  }
1085

1086
  return TSDB_CODE_SUCCESS;
14,539,421✔
1087
}
1088

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

1098
  pArray = taosArrayInit(tbNum, sizeof(STbSVersion));
175,028,495✔
1099
  if (NULL == pArray) {
175,028,730✔
1100
    return terrno;
4✔
1101
  }
1102

1103
  for (int32_t i = 0; i < tbNum; ++i) {
567,291,550✔
1104
    STbVerInfo* tbInfo = taosArrayGet(pTbArray, i);
392,260,624✔
1105
    if (NULL == tbInfo) {
392,260,978✔
1106
      code = terrno;
×
1107
      goto _return;
×
1108
    }
1109
    STbSVersion tbSver = {
392,260,978✔
1110
        .tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion, .rver = tbInfo->rversion};
392,260,931✔
1111
    if (NULL == taosArrayPush(pArray, &tbSver)) {
392,263,025✔
1112
      code = terrno;
×
1113
      goto _return;
×
1114
    }
1115
  }
1116

1117
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
175,030,926✔
1118
                           .requestId = pRequest->requestId,
175,031,235✔
1119
                           .requestObjRefId = pRequest->self,
175,030,893✔
1120
                           .mgmtEps = *epset};
1121

1122
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
175,031,627✔
1123

1124
_return:
175,028,386✔
1125

1126
  taosArrayDestroy(pArray);
175,028,199✔
1127
  return code;
175,029,987✔
1128
}
1129

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

1134
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog) {
77,133,789✔
1135
  return catalogAsyncUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
77,133,789✔
1136
}
1137

1138
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
929,494,922✔
1139
  if (NULL == pRequest->body.resInfo.execRes.res) {
929,494,922✔
1140
    return pRequest->code;
59,182,582✔
1141
  }
1142

1143
  SCatalog*     pCatalog = NULL;
870,251,935✔
1144
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
870,284,649✔
1145

1146
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
870,334,812✔
1147
  if (code) {
870,308,591✔
1148
    return code;
×
1149
  }
1150

1151
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
870,308,591✔
1152
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
870,350,812✔
1153

1154
  switch (pRes->msgType) {
870,353,384✔
1155
    case TDMT_VND_ALTER_TABLE:
4,273,099✔
1156
    case TDMT_MND_ALTER_STB: {
1157
      code = handleAlterTbExecRes(pRes->res, pCatalog);
4,273,099✔
1158
      break;
4,273,099✔
1159
    }
1160
    case TDMT_VND_CREATE_TABLE: {
48,914,542✔
1161
      SArray* pList = (SArray*)pRes->res;
48,914,542✔
1162
      int32_t num = taosArrayGetSize(pList);
48,915,929✔
1163
      for (int32_t i = 0; i < num; ++i) {
106,130,961✔
1164
        void* res = taosArrayGetP(pList, i);
57,208,774✔
1165
        // handleCreateTbExecRes will handle res == null
1166
        code = handleCreateTbExecRes(res, pCatalog);
57,214,958✔
1167
      }
1168
      break;
48,922,187✔
1169
    }
1170
    case TDMT_MND_CREATE_STB: {
447,125✔
1171
      code = handleCreateTbExecRes(pRes->res, pCatalog);
447,125✔
1172
      break;
447,125✔
1173
    }
1174
    case TDMT_VND_SUBMIT: {
641,651,890✔
1175
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
641,651,890✔
1176

1177
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
641,682,112✔
1178
      break;
641,669,550✔
1179
    }
1180
    case TDMT_SCH_QUERY:
175,028,647✔
1181
    case TDMT_SCH_MERGE_QUERY: {
1182
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
175,028,647✔
1183
      break;
175,026,761✔
1184
    }
1185
    default:
1,084✔
1186
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self,
1,084✔
1187
               pRequest->type, pRequest->requestId);
1188
      code = TSDB_CODE_APP_ERROR;
×
1189
  }
1190

1191
  return code;
870,338,722✔
1192
}
1193

1194
static bool incompletaFileParsing(SNode* pStmt) {
905,168,266✔
1195
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
905,168,266✔
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) {
84,850,400✔
1217
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
84,850,400✔
1218
    // return to client
1219
    doRequestCallback(pRequest, pRequest->code);
84,850,400✔
1220
    return;
84,846,772✔
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) {
919,638,495✔
1340
  SSqlCallbackWrapper* pWrapper = param;
919,638,495✔
1341
  SRequestObj*         pRequest = pWrapper->pRequest;
919,638,495✔
1342
  STscObj*             pTscObj = pRequest->pTscObj;
919,654,945✔
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;
919,666,277✔
1349
  if (pResult) {
919,670,369✔
1350
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
919,554,781✔
1351
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
919,561,394✔
1352
  }
1353

1354
  int32_t type = pRequest->type;
919,615,612✔
1355
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
919,580,853✔
1356
    if (pResult) {
685,570,662✔
1357
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
685,546,306✔
1358

1359
      // record the insert rows
1360
      if (TDMT_VND_SUBMIT == type) {
685,573,560✔
1361
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
632,919,992✔
1362
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
632,938,186✔
1363
      }
1364
    }
1365
    schedulerFreeJob(&pRequest->body.queryJob, 0);
685,598,235✔
1366
  }
1367

1368
  taosMemoryFree(pResult);
919,665,434✔
1369
  tscDebug("req:0x%" PRIx64 ", enter scheduler exec cb, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
919,618,636✔
1370
           pRequest->requestId);
1371

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

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

1392
  int32_t code1 = handleQueryExecRsp(pRequest);
919,549,961✔
1393
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
919,551,479✔
1394
    pRequest->code = code1;
×
1395
  }
1396

1397
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
1,824,750,253✔
1398
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
905,157,429✔
1399
    continueInsertFromCsv(pWrapper, pRequest);
13,590✔
1400
    return;
13,590✔
1401
  }
1402

1403
  if (pRequest->relation.nextRefId) {
919,559,723✔
1404
    handlePostSubQuery(pWrapper);
×
1405
  } else {
1406
    destorySqlCallbackWrapper(pWrapper);
919,571,454✔
1407
    pRequest->pWrapper = NULL;
919,500,829✔
1408

1409
    // return to client
1410
    doRequestCallback(pRequest, code);
919,529,961✔
1411
  }
1412
}
1413

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

1418
  if (pQuery->pRoot) {
9,958,734✔
1419
    pRequest->stmtType = pQuery->pRoot->type;
9,492,554✔
1420
    if (nodeType(pQuery->pRoot) == QUERY_NODE_DELETE_STMT) {
9,484,659✔
1421
      pRequest->secureDelete = ((SDeleteStmt*)pQuery->pRoot)->secureDelete;
×
1422
    }
1423
  }
1424

1425
  if (pQuery->pRoot && !pRequest->inRetry) {
9,959,651✔
1426
    STscObj*            pTscObj = pRequest->pTscObj;
9,485,004✔
1427
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
9,487,713✔
1428
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) {
9,490,729✔
1429
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
9,462,674✔
1430
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
23,501✔
1431
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
23,407✔
1432
    }
1433
  }
1434

1435
  pRequest->body.execMode = pQuery->execMode;
9,972,766✔
1436
  switch (pQuery->execMode) {
9,958,465✔
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:
475,075✔
1448
      if (!pRequest->validateOnly) {
475,075✔
1449
        code = execDdlQuery(pRequest, pQuery);
475,075✔
1450
      }
1451
      break;
475,075✔
1452
    case QUERY_EXEC_MODE_SCHEDULE: {
9,477,959✔
1453
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
9,477,959✔
1454
      if (NULL == pMnodeList) {
9,478,807✔
1455
        code = terrno;
×
1456
        break;
×
1457
      }
1458
      SQueryPlan* pDag = NULL;
9,478,807✔
1459
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
9,479,167✔
1460
      if (TSDB_CODE_SUCCESS == code) {
9,485,146✔
1461
        pRequest->body.subplanNum = pDag->numOfSubplans;
9,487,562✔
1462
        if (!pRequest->validateOnly) {
9,481,887✔
1463
          SArray* pNodeList = NULL;
9,481,471✔
1464
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
9,486,514✔
1465

1466
          if (TSDB_CODE_SUCCESS == code) {
9,487,889✔
1467
            code = sessMetricCheckValue((SSessMetric*)pRequest->pTscObj->pSessMetric, SESSION_MAX_CALL_VNODE_NUM,
9,491,827✔
1468
                                        taosArrayGetSize(pNodeList));
9,489,572✔
1469
          }
1470

1471
          if (TSDB_CODE_SUCCESS == code) {
9,487,461✔
1472
            code = scheduleQuery(pRequest, pDag, pNodeList);
9,487,461✔
1473
          }
1474
          taosArrayDestroy(pNodeList);
9,490,528✔
1475
        }
1476
      }
1477
      taosArrayDestroy(pMnodeList);
9,492,146✔
1478
      break;
9,494,736✔
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,968,889✔
1488
    qDestroyQuery(pQuery);
×
1489
  }
1490

1491
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
9,968,889✔
1492
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
29,799✔
1493
    if (TSDB_CODE_SUCCESS != ret) {
29,799✔
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,967,541✔
1500
    code = handleQueryExecRsp(pRequest);
9,964,863✔
1501
  }
1502

1503
  if (TSDB_CODE_SUCCESS != code) {
9,970,367✔
1504
    pRequest->code = code;
8,616✔
1505
  }
1506

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

1513
static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta,
920,013,297✔
1514
                                 SSqlCallbackWrapper* pWrapper) {
1515
  int32_t code = TSDB_CODE_SUCCESS;
920,013,297✔
1516
  pRequest->type = pQuery->msgType;
920,013,297✔
1517
  SArray*     pMnodeList = NULL;
920,028,637✔
1518
  SArray*     pNodeList = NULL;
920,028,637✔
1519
  SQueryPlan* pDag = NULL;
920,013,321✔
1520
  int64_t     st = taosGetTimestampUs();
920,052,641✔
1521

1522
  if (!pRequest->parseOnly) {
920,052,641✔
1523
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_PLAN);
1,840,116,786✔
1524

1525
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
920,215,278✔
1526
    if (NULL == pMnodeList) {
920,003,576✔
1527
      code = terrno;
×
1528
    }
1529
    SPlanContext cxt = {.queryId = pRequest->requestId,
1,018,881,700✔
1530
                        .acctId = pRequest->pTscObj->acctId,
920,102,422✔
1531
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
920,123,450✔
1532
                        .pAstRoot = pQuery->pRoot,
920,172,031✔
1533
                        .showRewrite = pQuery->showRewrite,
920,187,139✔
1534
                        .isView = pWrapper->pParseCtx->isView,
920,153,443✔
1535
                        .isAudit = pWrapper->pParseCtx->isAudit,
920,150,908✔
1536
                        .privInfo = pWrapper->pParseCtx->privInfo,
920,051,113✔
1537
                        .pMsg = pRequest->msgBuf,
920,122,155✔
1538
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1539
                        .pUser = pRequest->pTscObj->user,
920,101,861✔
1540
                        .userId = pRequest->pTscObj->userId,
920,085,663✔
1541
                        .sysInfo = pRequest->pTscObj->sysInfo,
920,048,427✔
1542
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
920,099,244✔
1543
                        .allocatorId = pRequest->stmtBindVersion > 0 ? 0 : pRequest->allocatorRefId};
920,084,930✔
1544
    if (TSDB_CODE_SUCCESS == code) {
920,090,722✔
1545
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
920,063,623✔
1546
    }
1547
    if (code) {
920,029,162✔
1548
      tscError("req:0x%" PRIx64 " requestId:0x%" PRIx64 ", failed to create query plan, code:%s msg:%s", pRequest->self,
290,254✔
1549
               pRequest->requestId, tstrerror(code), cxt.pMsg);
1550
    } else {
1551
      pRequest->body.subplanNum = pDag->numOfSubplans;
919,738,908✔
1552
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
919,807,572✔
1553
    }
1554
  }
1555

1556
  pRequest->metric.execStart = taosGetTimestampUs();
920,025,782✔
1557
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
920,002,045✔
1558

1559
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
920,068,125✔
1560
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
919,505,602✔
1561
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_SCHEDULE);
441,285,018✔
1562
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
220,652,185✔
1563
    }
1564

1565
    if (code == TSDB_CODE_SUCCESS) {
919,560,319✔
1566
      code = sessMetricCheckValue((SSessMetric*)pRequest->pTscObj->pSessMetric, SESSION_MAX_CALL_VNODE_NUM,
919,539,607✔
1567
                                  taosArrayGetSize(pNodeList));
919,568,686✔
1568
    }
1569

1570
    if (code == TSDB_CODE_SUCCESS) {
919,561,403✔
1571
      SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
919,557,425✔
1572
                               .requestId = pRequest->requestId,
919,612,225✔
1573
                               .requestObjRefId = pRequest->self};
919,567,579✔
1574
      SSchedulerReq    req = {
969,001,263✔
1575
             .syncReq = false,
1576
             .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
919,512,777✔
1577
             .pConn = &conn,
1578
             .pNodeList = pNodeList,
1579
             .pDag = pDag,
1580
             .allocatorRefId = pRequest->allocatorRefId,
919,512,777✔
1581
             .sql = pRequest->sqlstr,
919,472,366✔
1582
             .startTs = pRequest->metric.start,
919,491,460✔
1583
             .execFp = schedulerExecCb,
1584
             .cbParam = pWrapper,
1585
             .chkKillFp = chkRequestKilled,
1586
             .chkKillParam = (void*)pRequest->self,
919,571,853✔
1587
             .pExecRes = NULL,
1588
             .source = pRequest->source,
919,476,464✔
1589
             .secureDelete = pRequest->secureDelete,
919,513,677✔
1590
             .pWorkerCb = getTaskPoolWorkerCb(),
919,457,785✔
1591
      };
1592

1593
      if (TSDB_CODE_SUCCESS == code) {
919,467,862✔
1594
        CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_EXECUTE);
1,839,205,309✔
1595
        code = schedulerExecJob(&req, &pRequest->body.queryJob);
919,578,127✔
1596
      }
1597
      taosArrayDestroy(pNodeList);
919,478,947✔
1598
      taosArrayDestroy(pMnodeList);
919,607,162✔
1599
      return code;
919,612,579✔
1600
    }
1601
  }
1602

1603
  qDestroyQueryPlan(pDag);
633,222✔
1604
  tscDebug("req:0x%" PRIx64 ", plan not executed, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
511,902✔
1605
           pRequest->requestId);
1606
  destorySqlCallbackWrapper(pWrapper);
511,902✔
1607
  pRequest->pWrapper = NULL;
511,902✔
1608
  if (TSDB_CODE_SUCCESS != code) {
511,902✔
1609
    pRequest->code = code;
294,232✔
1610
  }
1611

1612
  doRequestCallback(pRequest, code);
511,902✔
1613

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

1618
  return code;
508,711✔
1619
}
1620

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

1624
  if (pRequest->parseOnly) {
1,045,892,439✔
1625
    doRequestCallback(pRequest, 0);
338,450✔
1626
    return;
338,450✔
1627
  }
1628

1629
  pRequest->body.execMode = pQuery->execMode;
1,045,618,829✔
1630
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
1,045,597,010✔
1631
    destorySqlCallbackWrapper(pWrapper);
125,537,065✔
1632
    pRequest->pWrapper = NULL;
125,539,994✔
1633
  }
1634

1635
  if (pQuery->pRoot && !pRequest->inRetry) {
1,045,539,677✔
1636
    STscObj*            pTscObj = pRequest->pTscObj;
1,045,593,412✔
1637
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
1,045,655,670✔
1638
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
1,045,609,621✔
1639
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
698,952,043✔
1640
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
632,386,949✔
1641
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
413,277,016✔
1642
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
164,664,712✔
1643
    }
1644
  }
1645

1646
  switch (pQuery->execMode) {
1,045,692,952✔
1647
    case QUERY_EXEC_MODE_LOCAL:
7,030,605✔
1648
      asyncExecLocalCmd(pRequest, pQuery);
7,030,605✔
1649
      break;
7,030,606✔
1650
    case QUERY_EXEC_MODE_RPC:
117,839,417✔
1651
      code = asyncExecDdlQuery(pRequest, pQuery);
117,839,417✔
1652
      break;
117,836,090✔
1653
    case QUERY_EXEC_MODE_SCHEDULE: {
920,085,272✔
1654
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
920,085,272✔
1655
      break;
920,110,513✔
1656
    }
1657
    case QUERY_EXEC_MODE_EMPTY_RESULT:
663,297✔
1658
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
663,297✔
1659
      doRequestCallback(pRequest, 0);
663,297✔
1660
      break;
663,297✔
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) {
15,567✔
1669
  SCatalog* pCatalog = NULL;
15,567✔
1670
  int32_t   code = 0;
15,567✔
1671
  int32_t   dbNum = taosArrayGetSize(pRequest->dbList);
15,567✔
1672
  int32_t   tblNum = taosArrayGetSize(pRequest->tableList);
15,567✔
1673

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

1719
  if (isView) {
5,564,645✔
1720
    for (int32_t i = 0; i < tbNum; ++i) {
799,696✔
1721
      SName* pViewName = taosArrayGet(tbList, i);
399,848✔
1722
      char   dbFName[TSDB_DB_FNAME_LEN];
396,284✔
1723
      if (NULL == pViewName) {
399,848✔
1724
        continue;
×
1725
      }
1726
      (void)tNameGetFullDbName(pViewName, dbFName);
399,848✔
1727
      TSC_ERR_RET(catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0));
399,848✔
1728
    }
1729
  } else {
1730
    for (int32_t i = 0; i < tbNum; ++i) {
8,139,420✔
1731
      SName* pTbName = taosArrayGet(tbList, i);
2,974,623✔
1732
      TSC_ERR_RET(catalogRemoveTableMeta(pCatalog, pTbName));
2,974,623✔
1733
    }
1734
  }
1735

1736
  return TSDB_CODE_SUCCESS;
5,564,645✔
1737
}
1738

1739
int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) {
101,007,299✔
1740
  pEpSet->version = 0;
101,007,299✔
1741

1742
  // init mnode ip set
1743
  SEpSet* mgmtEpSet = &(pEpSet->epSet);
101,007,459✔
1744
  mgmtEpSet->numOfEps = 0;
101,007,469✔
1745
  mgmtEpSet->inUse = 0;
101,007,497✔
1746

1747
  if (firstEp && firstEp[0] != 0) {
101,007,341✔
1748
    if (strlen(firstEp) >= TSDB_EP_LEN) {
101,010,021✔
1749
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1750
      return -1;
×
1751
    }
1752

1753
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
101,010,021✔
1754
    if (code != TSDB_CODE_SUCCESS) {
101,009,564✔
1755
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1756
      return terrno;
×
1757
    }
1758
    // uint32_t addr = 0;
1759
    SIpAddr addr = {0};
101,009,564✔
1760
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
101,010,122✔
1761
    if (code) {
101,007,957✔
1762
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
188✔
1763
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1764
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
204✔
1765
    } else {
1766
      mgmtEpSet->numOfEps++;
101,009,888✔
1767
    }
1768
  }
1769

1770
  if (secondEp && secondEp[0] != 0) {
101,006,857✔
1771
    if (strlen(secondEp) >= TSDB_EP_LEN) {
2,186,163✔
1772
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1773
      return terrno;
×
1774
    }
1775

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

1791
  if (mgmtEpSet->numOfEps == 0) {
101,006,802✔
1792
    terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
204✔
1793
    return TSDB_CODE_RPC_NETWORK_UNAVAIL;
204✔
1794
  }
1795

1796
  return 0;
101,006,552✔
1797
}
1798

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

1807
  SRequestObj* pRequest = NULL;
101,009,896✔
1808
  code = createRequest((*pTscObj)->id, TDMT_MND_CONNECT, 0, &pRequest);
101,009,896✔
1809
  if (TSDB_CODE_SUCCESS != code) {
101,010,096✔
1810
    destroyTscObj(*pTscObj);
×
1811
    return code;
×
1812
  }
1813

1814
  pRequest->sqlstr = taosStrdup("taos_connect");
101,010,096✔
1815
  if (pRequest->sqlstr) {
101,008,602✔
1816
    pRequest->sqlLen = strlen(pRequest->sqlstr);
101,008,602✔
1817
  } else {
1818
    return terrno;
×
1819
  }
1820

1821
  SMsgSendInfo* body = NULL;
101,008,602✔
1822
  code = buildConnectMsg(pRequest, &body, totpCode);
101,008,602✔
1823
  if (TSDB_CODE_SUCCESS != code) {
101,006,836✔
1824
    destroyTscObj(*pTscObj);
×
1825
    return code;
×
1826
  }
1827

1828
  // int64_t transporterId = 0;
1829
  SEpSet epset = getEpSet_s(&(*pTscObj)->pAppInfo->mgmtEp);
101,006,836✔
1830
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &epset, NULL, body);
101,008,842✔
1831
  if (TSDB_CODE_SUCCESS != code) {
101,006,875✔
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)) {
101,006,875✔
UNCOV
1837
    destroyTscObj(*pTscObj);
×
1838
    tscError("failed to wait sem, code:%s", terrstr());
×
1839
    return terrno;
×
1840
  }
1841
  if (pRequest->code != TSDB_CODE_SUCCESS) {
101,008,118✔
1842
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
21,188✔
1843
    tscError("failed to connect to server, reason: %s", errorMsg);
21,188✔
1844

1845
    terrno = pRequest->code;
21,188✔
1846
    destroyRequest(pRequest);
21,188✔
1847
    taos_close_internal(*pTscObj);
21,188✔
1848
    *pTscObj = NULL;
21,188✔
1849
    return terrno;
21,188✔
1850
  }
1851
  if (connType == CONN_TYPE__AUTH_TEST) {
100,986,930✔
1852
    terrno = TSDB_CODE_SUCCESS;
98✔
1853
    destroyRequest(pRequest);
98✔
1854
    taos_close_internal(*pTscObj);
98✔
1855
    *pTscObj = NULL;
1,610✔
1856
    return TSDB_CODE_SUCCESS;
1,610✔
1857
  }
1858

1859
  tscInfo("conn:0x%" PRIx64 ", connection is opening, connId:%u, dnodeConn:%p, QID:0x%" PRIx64, (*pTscObj)->id,
100,986,832✔
1860
          (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId);
1861
  destroyRequest(pRequest);
100,987,740✔
1862
  return code;
100,985,181✔
1863
}
1864

1865
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo, int32_t totpCode) {
101,008,587✔
1866
  *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
101,008,587✔
1867
  if (*pMsgSendInfo == NULL) {
101,009,893✔
1868
    return terrno;
×
1869
  }
1870

1871
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
101,009,893✔
1872

1873
  (*pMsgSendInfo)->requestObjRefId = pRequest->self;
101,009,893✔
1874
  (*pMsgSendInfo)->requestId = pRequest->requestId;
101,009,893✔
1875
  (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType);
101,009,893✔
1876
  (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self));
101,008,407✔
1877
  if (NULL == (*pMsgSendInfo)->param) {
101,009,888✔
1878
    taosMemoryFree(*pMsgSendInfo);
×
1879
    return terrno;
×
1880
  }
1881

1882
  *(int64_t*)(*pMsgSendInfo)->param = pRequest->self;
101,009,248✔
1883

1884
  SConnectReq connectReq = {0};
101,009,248✔
1885
  STscObj*    pObj = pRequest->pTscObj;
101,009,303✔
1886

1887
  char* db = getDbOfConnection(pObj);
101,009,303✔
1888
  if (db != NULL) {
101,007,188✔
1889
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
490,668✔
1890
  } else if (terrno) {
100,516,520✔
1891
    taosMemoryFree(*pMsgSendInfo);
×
1892
    return terrno;
×
1893
  }
1894
  taosMemoryFreeClear(db);
101,009,886✔
1895

1896
  connectReq.connType = pObj->connType;
101,009,939✔
1897
  connectReq.pid = appInfo.pid;
101,009,884✔
1898
  connectReq.startTime = appInfo.startTime;
101,009,884✔
1899
  connectReq.totpCode = totpCode;
101,009,884✔
1900
  connectReq.connectTime = taosGetTimestampMs();
101,006,233✔
1901

1902
  tstrncpy(connectReq.app, appInfo.appName, sizeof(connectReq.app));
101,006,233✔
1903
  tstrncpy(connectReq.user, pObj->user, sizeof(connectReq.user));
101,005,593✔
1904
  tstrncpy(connectReq.passwd, pObj->pass, sizeof(connectReq.passwd));
101,006,233✔
1905
  tstrncpy(connectReq.token, pObj->token, sizeof(connectReq.token));
101,006,178✔
1906
  tstrncpy(connectReq.sVer, td_version, sizeof(connectReq.sVer));
101,005,593✔
1907
  tSignConnectReq(&connectReq);
101,006,233✔
1908

1909
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
101,009,013✔
1910
  void*   pReq = taosMemoryMalloc(contLen);
101,006,852✔
1911
  if (NULL == pReq) {
101,009,182✔
1912
    taosMemoryFree(*pMsgSendInfo);
×
1913
    return terrno;
×
1914
  }
1915

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

1922
  (*pMsgSendInfo)->msgInfo.len = contLen;
101,006,418✔
1923
  (*pMsgSendInfo)->msgInfo.pData = pReq;
101,005,951✔
1924
  return TSDB_CODE_SUCCESS;
101,005,887✔
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,197,360✔
1933
    case TARGET_TYPE_MNODE:
526✔
1934
      if (NULL == pTscObj) {
526✔
1935
        tscError("mnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1936
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1937
        return;
988✔
1938
      }
1939

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

1955
      SCatalog* pCatalog = NULL;
3,914,217✔
1956
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
3,914,217✔
1957
      if (code != TSDB_CODE_SUCCESS) {
3,914,164✔
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,914,164✔
1964
      if (code != TSDB_CODE_SUCCESS) {
3,914,251✔
UNCOV
1965
        tscError("fail to update catalog vg epset, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1966
                 tstrerror(code));
1967
        return;
×
1968
      }
1969
      taosMemoryFreeClear(pSendInfo->target.dbFName);
3,914,251✔
1970
      break;
3,914,220✔
1971
    }
1972
    default:
290,629✔
1973
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
290,629✔
1974
      break;
291,434✔
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");
597,153✔
1982
    rpcFreeCont(pMsg->pCont);
597,153✔
1983
    taosMemoryFree(pEpSet);
597,153✔
1984
    return TSDB_CODE_TSC_INTERNAL_ERROR;
597,153✔
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,975,671,469✔
1998
    if (pRequest) {
1,975,654,177✔
1999
      if (pRequest->self != pSendInfo->requestObjRefId) {
1,959,416,963✔
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,959,414,919✔
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,959,359,646✔
2037
    if (TSDB_CODE_SUCCESS != code) {
1,959,445,707✔
2038
      tscError("doProcessMsgFromServer taosReleaseRef failed");
89✔
2039
      terrno = code;
89✔
2040
      pMsg->code = code;
89✔
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,207,208✔
2064
    if (NULL == tEpSet) {
4,207,018✔
2065
      code = terrno;
×
2066
      pMsg->code = terrno;
×
2067
      goto _exit;
×
2068
    }
2069
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
4,207,018✔
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) {
100,938,120✔
2076
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
2077
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
100,938,053✔
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;
406,700✔
2099
    taosMemoryFree(arg);
406,700✔
2100
    goto _exit;
333,872✔
2101
  }
2102
  return;
2,147,483,647✔
2103

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

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

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

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

2137
  return NULL;
938✔
2138
}
2139

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

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

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

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

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

2172
  return NULL;
1,670✔
2173
}
2174

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

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

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

2197
  return NULL;
160✔
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);
12,749✔
2213
          pResultInfo->row[i] = blobDataVal(pStart);
396✔
2214
        } else {
2215
          pResultInfo->length[i] = varDataLen(pStart);
2,147,483,647✔
2216
          pResultInfo->row[i] = varDataVal(pStart);
2,147,483,647✔
2217
        }
2218
      } else {
2219
        pResultInfo->row[i] = NULL;
300,933,967✔
2220
        pResultInfo->length[i] = 0;
300,981,024✔
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,305,353,833✔
2228
        pResultInfo->length[i] = 0;
1,305,721,640✔
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) {
235,546,620✔
2285
  tsem_t* sem = param;
235,546,620✔
2286
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
235,546,620✔
2287
    tscError("failed to post sem, code:%s", terrstr());
×
2288
  }
2289
}
235,547,161✔
2290

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

2296
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
1,826,674,355✔
2297
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
1,826,674,531✔
2298
    // All data has returned to App already, no need to try again
2299
    if (pResultInfo->completed) {
341,774,190✔
2300
      pResultInfo->numOfRows = 0;
106,227,307✔
2301
      CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
212,454,441✔
2302
      return NULL;
106,227,290✔
2303
    }
2304

2305
    // convert ucs4 to native multi-bytes string
2306
    pResultInfo->convertUcs4 = convertUcs4;
235,547,013✔
2307
    tsem_t sem;
234,808,226✔
2308
    if (TSDB_CODE_SUCCESS != tsem_init(&sem, 0, 0)) {
235,546,932✔
2309
      tscError("failed to init sem, code:%s", terrstr());
×
2310
    }
2311
    taos_fetch_rows_a(pRequest, syncFetchFn, &sem);
235,547,048✔
2312
    if (TSDB_CODE_SUCCESS != tsem_wait(&sem)) {
235,547,161✔
2313
      tscError("failed to wait sem, code:%s", terrstr());
×
2314
    }
2315
    if (TSDB_CODE_SUCCESS != tsem_destroy(&sem)) {
235,546,624✔
2316
      tscError("failed to destroy sem, code:%s", terrstr());
×
2317
    }
2318
    pRequest->inCallback = false;
235,545,350✔
2319
  }
2320

2321
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
1,720,447,192✔
2322
    return NULL;
12,432,239✔
2323
  } else {
2324
    if (setupOneRowPtr) {
1,708,015,089✔
2325
      doSetOneRowPtr(pResultInfo);
1,483,482,397✔
2326
      pResultInfo->current += 1;
1,483,482,549✔
2327
    }
2328

2329
    return pResultInfo->row;
1,708,015,203✔
2330
  }
2331
}
2332

2333
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
349,763,606✔
2334
  if (pResInfo->row == NULL) {
349,763,606✔
2335
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
228,449,789✔
2336
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
228,444,744✔
2337
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
228,442,221✔
2338
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
228,442,945✔
2339

2340
    if (pResInfo->row == NULL || pResInfo->pCol == NULL || pResInfo->length == NULL || pResInfo->convertBuf == NULL) {
228,446,896✔
2341
      taosMemoryFree(pResInfo->row);
3,699✔
2342
      taosMemoryFree(pResInfo->pCol);
×
2343
      taosMemoryFree(pResInfo->length);
×
2344
      taosMemoryFree(pResInfo->convertBuf);
×
2345
      return terrno;
×
2346
    }
2347
  }
2348

2349
  return TSDB_CODE_SUCCESS;
349,762,911✔
2350
}
2351

2352
static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength, bool isStmt) {
349,518,847✔
2353
  int32_t idx = -1;
349,518,847✔
2354
  iconv_t conv = taosAcquireConv(&idx, C2M, pResultInfo->charsetCxt);
349,520,322✔
2355
  if (conv == (iconv_t)-1) return TSDB_CODE_TSC_INTERNAL_ERROR;
349,512,926✔
2356

2357
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,058,143,590✔
2358
    int32_t type = pResultInfo->fields[i].type;
1,708,644,845✔
2359
    int32_t schemaBytes =
2360
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
1,708,647,986✔
2361

2362
    if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) {
1,708,641,670✔
2363
      char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]);
104,441,532✔
2364
      if (p == NULL) {
104,441,532✔
2365
        taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2366
        return terrno;
×
2367
      }
2368

2369
      pResultInfo->convertBuf[i] = p;
104,441,532✔
2370

2371
      SResultColumn* pCol = &pResultInfo->pCol[i];
104,441,532✔
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(
36,736✔
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);
36,736✔
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];
104,441,466✔
2393
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
104,441,466✔
2394
    }
2395
  }
2396
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
349,516,816✔
2397
  return TSDB_CODE_SUCCESS;
349,519,533✔
2398
}
2399

2400
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
349,516,554✔
2401
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,058,135,839✔
2402
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
1,708,642,356✔
2403
    TAOS_FIELD*   pField = pResultInfo->userFields + i;
1,708,644,694✔
2404
    int32_t       type = pFieldE->type;
1,708,637,793✔
2405
    int32_t       bufLen = 0;
1,708,640,361✔
2406
    char*         p = NULL;
1,708,640,361✔
2407
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
1,708,640,361✔
2408
      continue;
1,706,729,465✔
2409
    } else {
2410
      bufLen = 64;
1,900,258✔
2411
      p = taosMemoryRealloc(pResultInfo->convertBuf[i], bufLen * pResultInfo->numOfRows);
1,900,258✔
2412
      pFieldE->bytes = bufLen;
1,900,258✔
2413
      pField->bytes = bufLen;
1,900,258✔
2414
    }
2415
    if (!p) return terrno;
1,900,258✔
2416
    pResultInfo->convertBuf[i] = p;
1,900,258✔
2417

2418
    for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
1,098,383,848✔
2419
      int32_t code = decimalToStr((DecimalWord*)(pResultInfo->pCol[i].pData + j * tDataTypes[type].bytes), type,
1,096,483,590✔
2420
                                  pFieldE->precision, pFieldE->scale, p, bufLen);
1,096,483,590✔
2421
      p += bufLen;
1,096,483,590✔
2422
      if (TSDB_CODE_SUCCESS != code) {
1,096,483,590✔
2423
        return code;
×
2424
      }
2425
    }
2426
    pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
1,900,258✔
2427
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
1,900,258✔
2428
  }
2429
  return 0;
349,511,057✔
2430
}
2431

2432
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols) {
456,884✔
2433
  return sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) * 3 + sizeof(uint64_t) +
913,176✔
2434
         numOfCols * (sizeof(int8_t) + sizeof(int32_t));
456,292✔
2435
}
2436

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

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

2456
  char* pStart = p + len;
228,442✔
2457
  for (int32_t i = 0; i < numOfCols; ++i) {
973,778✔
2458
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
745,336✔
2459

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

2466
      int32_t estimateColLen = 0;
265,170✔
2467
      for (int32_t j = 0; j < numOfRows; ++j) {
1,279,928✔
2468
        if (offset[j] == -1) {
1,014,758✔
2469
          continue;
56,812✔
2470
        }
2471
        char* data = offset[j] + pStart;
957,946✔
2472

2473
        int32_t jsonInnerType = *data;
957,946✔
2474
        char*   jsonInnerData = data + CHAR_BYTES;
957,946✔
2475
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
957,946✔
2476
          estimateColLen += (VARSTR_HEADER_SIZE + strlen(TSDB_DATA_NULL_STR_L));
13,296✔
2477
        } else if (tTagIsJson(data)) {
944,650✔
2478
          estimateColLen += (VARSTR_HEADER_SIZE + ((const STag*)(data))->len);
232,808✔
2479
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
711,842✔
2480
          estimateColLen += varDataTLen(jsonInnerData) + CHAR_BYTES * 2;
661,982✔
2481
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
49,860✔
2482
          estimateColLen += (VARSTR_HEADER_SIZE + 32);
36,564✔
2483
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
13,296✔
2484
          estimateColLen += (VARSTR_HEADER_SIZE + 5);
13,296✔
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);
265,170✔
2493
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
480,166✔
2494
      int32_t lenTmp = numOfRows * sizeof(int32_t);
66,540✔
2495
      len += (lenTmp + colLen);
66,540✔
2496
      pStart += lenTmp;
66,540✔
2497
    } else {
2498
      int32_t lenTmp = BitmapLen(pResultInfo->numOfRows);
413,626✔
2499
      len += (lenTmp + colLen);
413,626✔
2500
      pStart += lenTmp;
413,626✔
2501
    }
2502
    pStart += colLen;
745,336✔
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);
228,442✔
2508
  return len;
228,442✔
2509
}
2510

2511
static int32_t doConvertJson(SReqResultInfo* pResultInfo) {
349,756,195✔
2512
  int32_t numOfRows = pResultInfo->numOfRows;
349,756,195✔
2513
  int32_t numOfCols = pResultInfo->numOfCols;
349,757,560✔
2514
  bool    needConvert = false;
349,763,928✔
2515
  for (int32_t i = 0; i < numOfCols; ++i) {
2,059,200,188✔
2516
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
1,709,668,727✔
2517
      needConvert = true;
228,442✔
2518
      break;
228,442✔
2519
    }
2520
  }
2521

2522
  if (!needConvert) {
349,759,903✔
2523
    return TSDB_CODE_SUCCESS;
349,531,461✔
2524
  }
2525

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

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

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

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

2551
  p += len;
228,442✔
2552
  p1 += len;
228,442✔
2553
  totalLen += len;
228,442✔
2554

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

2563
  char* pStart = p;
228,442✔
2564
  char* pStart1 = p1;
228,442✔
2565
  for (int32_t i = 0; i < numOfCols; ++i) {
973,778✔
2566
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
745,336✔
2567
    int32_t colLen1 = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength1[i]) : colLength1[i];
745,336✔
2568
    if (colLen >= dataLen) {
745,336✔
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) {
745,336✔
2573
      int32_t* offset = (int32_t*)pStart;
265,170✔
2574
      int32_t* offset1 = (int32_t*)pStart1;
265,170✔
2575
      len = numOfRows * sizeof(int32_t);
265,170✔
2576
      (void)memcpy(pStart1, pStart, len);
265,170✔
2577
      pStart += len;
265,170✔
2578
      pStart1 += len;
265,170✔
2579
      totalLen += len;
265,170✔
2580

2581
      len = 0;
265,170✔
2582
      for (int32_t j = 0; j < numOfRows; ++j) {
1,279,928✔
2583
        if (offset[j] == -1) {
1,014,758✔
2584
          continue;
56,812✔
2585
        }
2586
        char* data = offset[j] + pStart;
957,946✔
2587

2588
        int32_t jsonInnerType = *data;
957,946✔
2589
        char*   jsonInnerData = data + CHAR_BYTES;
957,946✔
2590
        char    dst[TSDB_MAX_JSON_TAG_LEN] = {0};
957,946✔
2591
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
957,946✔
2592
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s", TSDB_DATA_NULL_STR_L);
13,296✔
2593
          varDataSetLen(dst, strlen(varDataVal(dst)));
13,296✔
2594
        } else if (tTagIsJson(data)) {
944,650✔
2595
          char* jsonString = NULL;
232,808✔
2596
          parseTagDatatoJson(data, &jsonString, pResultInfo->charsetCxt);
232,808✔
2597
          if (jsonString == NULL) {
232,808✔
2598
            tscError("doConvertJson error: parseTagDatatoJson failed");
×
2599
            return terrno;
×
2600
          }
2601
          STR_TO_VARSTR(dst, jsonString);
232,808✔
2602
          taosMemoryFree(jsonString);
232,808✔
2603
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
711,842✔
2604
          *(char*)varDataVal(dst) = '\"';
661,982✔
2605
          char    tmp[TSDB_MAX_JSON_TAG_LEN] = {0};
661,982✔
2606
          int32_t length = taosUcs4ToMbs((TdUcs4*)varDataVal(jsonInnerData), varDataLen(jsonInnerData), varDataVal(tmp),
661,982✔
2607
                                         pResultInfo->charsetCxt);
2608
          if (length <= 0) {
661,982✔
2609
            tscError("charset:%s to %s. convert failed.", DEFAULT_UNICODE_ENCODEC,
554✔
2610
                     pResultInfo->charsetCxt != NULL ? ((SConvInfo*)(pResultInfo->charsetCxt))->charset : tsCharset);
2611
            length = 0;
554✔
2612
          }
2613
          int32_t escapeLength = escapeToPrinted(varDataVal(dst) + CHAR_BYTES, TSDB_MAX_JSON_TAG_LEN - CHAR_BYTES * 2,
661,982✔
2614
                                                 varDataVal(tmp), length);
2615
          varDataSetLen(dst, escapeLength + CHAR_BYTES * 2);
661,982✔
2616
          *(char*)POINTER_SHIFT(varDataVal(dst), escapeLength + CHAR_BYTES) = '\"';
661,982✔
2617
          tscError("value:%s.", varDataVal(dst));
661,982✔
2618
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
49,860✔
2619
          double jsonVd = *(double*)(jsonInnerData);
36,564✔
2620
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%.9lf", jsonVd);
36,564✔
2621
          varDataSetLen(dst, strlen(varDataVal(dst)));
36,564✔
2622
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
13,296✔
2623
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s",
13,296✔
2624
                         (*((char*)jsonInnerData) == 1) ? "true" : "false");
13,296✔
2625
          varDataSetLen(dst, strlen(varDataVal(dst)));
13,296✔
2626
        } else {
2627
          tscError("doConvertJson error: invalid type:%d", jsonInnerType);
×
2628
          return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2629
        }
2630

2631
        offset1[j] = len;
957,946✔
2632
        (void)memcpy(pStart1 + len, dst, varDataTLen(dst));
957,946✔
2633
        len += varDataTLen(dst);
957,946✔
2634
      }
2635
      colLen1 = len;
265,170✔
2636
      totalLen += colLen1;
265,170✔
2637
      colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len;
265,170✔
2638
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
480,166✔
2639
      len = numOfRows * sizeof(int32_t);
66,540✔
2640
      (void)memcpy(pStart1, pStart, len);
66,540✔
2641
      pStart += len;
66,540✔
2642
      pStart1 += len;
66,540✔
2643
      totalLen += len;
66,540✔
2644
      totalLen += colLen;
66,540✔
2645
      (void)memcpy(pStart1, pStart, colLen);
66,540✔
2646
    } else {
2647
      len = BitmapLen(pResultInfo->numOfRows);
413,626✔
2648
      (void)memcpy(pStart1, pStart, len);
413,626✔
2649
      pStart += len;
413,626✔
2650
      pStart1 += len;
413,626✔
2651
      totalLen += len;
413,626✔
2652
      totalLen += colLen;
413,626✔
2653
      (void)memcpy(pStart1, pStart, colLen);
413,626✔
2654
    }
2655
    pStart += colLen;
745,336✔
2656
    pStart1 += colLen1;
745,336✔
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);
228,442✔
2663

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

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

2676
  if (pResultInfo->numOfRows == 0) {
376,668,304✔
2677
    return TSDB_CODE_SUCCESS;
26,904,396✔
2678
  }
2679

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

2685
  int32_t code = doPrepareResPtr(pResultInfo);
349,763,075✔
2686
  if (code != TSDB_CODE_SUCCESS) {
349,761,666✔
2687
    return code;
×
2688
  }
2689
  code = doConvertJson(pResultInfo);
349,761,666✔
2690
  if (code != TSDB_CODE_SUCCESS) {
349,751,582✔
2691
    return code;
×
2692
  }
2693

2694
  char* p = (char*)pResultInfo->pData;
349,751,582✔
2695

2696
  // version:
2697
  int32_t blockVersion = *(int32_t*)p;
349,752,013✔
2698
  p += sizeof(int32_t);
349,757,962✔
2699

2700
  int32_t dataLen = *(int32_t*)p;
349,758,752✔
2701
  p += sizeof(int32_t);
349,758,393✔
2702

2703
  int32_t rows = *(int32_t*)p;
349,758,752✔
2704
  p += sizeof(int32_t);
349,758,688✔
2705

2706
  int32_t cols = *(int32_t*)p;
349,757,365✔
2707
  p += sizeof(int32_t);
349,758,195✔
2708

2709
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
349,759,819✔
2710
    tscError("setResultDataPtr paras error:rows;%d numOfRows:%" PRId64 " cols:%d numOfCols:%d", rows,
5,982✔
2711
             pResultInfo->numOfRows, cols, pResultInfo->numOfCols);
2712
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2713
  }
2714

2715
  int32_t hasColumnSeg = *(int32_t*)p;
349,757,048✔
2716
  p += sizeof(int32_t);
349,763,254✔
2717

2718
  uint64_t groupId = taosGetUInt64Aligned((uint64_t*)p);
349,762,352✔
2719
  p += sizeof(uint64_t);
349,762,352✔
2720

2721
  // check fields
2722
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,059,479,486✔
2723
    int8_t type = *(int8_t*)p;
1,709,733,461✔
2724
    p += sizeof(int8_t);
1,709,727,554✔
2725

2726
    int32_t bytes = *(int32_t*)p;
1,709,730,824✔
2727
    p += sizeof(int32_t);
1,709,729,854✔
2728

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

2734
  int32_t* colLength = (int32_t*)p;
349,761,985✔
2735
  p += sizeof(int32_t) * pResultInfo->numOfCols;
349,761,985✔
2736

2737
  char* pStart = p;
349,762,240✔
2738
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
2,059,513,510✔
2739
    if ((pStart - pResultInfo->pData) >= dataLen) {
1,709,759,555✔
2740
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2741
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2742
    }
2743
    if (blockVersion == BLOCK_VERSION_1) {
1,709,745,500✔
2744
      colLength[i] = htonl(colLength[i]);
1,226,177,051✔
2745
    }
2746
    if (colLength[i] >= dataLen) {
1,709,747,219✔
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,709,749,803✔
2751
      tscError("invalid type %d", pResultInfo->fields[i].type);
252✔
2752
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2753
    }
2754
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,709,755,003✔
2755
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
372,191,487✔
2756
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
372,189,940✔
2757
    } else {
2758
      pResultInfo->pCol[i].nullbitmap = pStart;
1,337,578,301✔
2759
      pStart += BitmapLen(pResultInfo->numOfRows);
1,337,586,467✔
2760
    }
2761

2762
    pResultInfo->pCol[i].pData = pStart;
1,709,771,373✔
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,709,757,750✔
2766

2767
    pStart += colLength[i];
1,709,758,152✔
2768
  }
2769

2770
  p = pStart;
349,766,291✔
2771
  // bool blankFill = *(bool*)p;
2772
  p += sizeof(bool);
349,766,291✔
2773
  int32_t offset = p - pResultInfo->pData;
349,766,646✔
2774
  if (offset > dataLen) {
349,764,800✔
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) {
349,764,800✔
2781
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
349,519,618✔
2782
  }
2783
#endif
2784
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
349,764,781✔
2785
    code = convertDecimalType(pResultInfo);
349,520,000✔
2786
  }
2787
  return code;
349,755,475✔
2788
}
2789

2790
char* getDbOfConnection(STscObj* pObj) {
1,359,113,101✔
2791
  terrno = TSDB_CODE_SUCCESS;
1,359,113,101✔
2792
  char* p = NULL;
1,359,135,401✔
2793
  (void)taosThreadMutexLock(&pObj->mutex);
1,359,135,401✔
2794
  size_t len = strlen(pObj->db);
1,359,177,626✔
2795
  if (len > 0) {
1,359,180,206✔
2796
    p = taosStrndup(pObj->db, tListLen(pObj->db));
938,723,865✔
2797
    if (p == NULL) {
938,718,681✔
2798
      tscError("failed to taosStrndup db name");
×
2799
    }
2800
  }
2801

2802
  (void)taosThreadMutexUnlock(&pObj->mutex);
1,359,175,022✔
2803
  return p;
1,359,115,087✔
2804
}
2805

2806
void setConnectionDB(STscObj* pTscObj, const char* db) {
100,726,478✔
2807
  if (db == NULL || pTscObj == NULL) {
100,726,478✔
2808
    tscError("setConnectionDB para is NULL");
30✔
2809
    return;
×
2810
  }
2811

2812
  (void)taosThreadMutexLock(&pTscObj->mutex);
100,777,228✔
2813
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
100,803,803✔
2814
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
100,803,617✔
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,
291,378,122✔
2828
                              bool isStmt) {
2829
  if (pResultInfo == NULL || pRsp == NULL) {
291,378,122✔
2830
    tscError("setQueryResultFromRsp paras is null");
149✔
2831
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2832
  }
2833

2834
  taosMemoryFreeClear(pResultInfo->pRspMsg);
291,377,973✔
2835
  pResultInfo->pRspMsg = (const char*)pRsp;
291,377,975✔
2836
  pResultInfo->numOfRows = htobe64(pRsp->numOfRows);
291,378,200✔
2837
  pResultInfo->current = 0;
291,378,252✔
2838
  pResultInfo->completed = (pRsp->completed == 1);
291,378,252✔
2839
  pResultInfo->precision = pRsp->precision;
291,378,139✔
2840

2841
  // decompress data if needed
2842
  int32_t payloadLen = htonl(pRsp->payloadLen);
291,378,053✔
2843

2844
  if (pRsp->compressed) {
291,377,930✔
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) {
291,377,951✔
2867
    int32_t compLen = *(int32_t*)pRsp->data;
264,474,816✔
2868
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
264,474,817✔
2869

2870
    char* pStart = (char*)pRsp->data + sizeof(int32_t) * 2;
264,474,872✔
2871

2872
    if (pRsp->compressed && compLen < rawLen) {
264,474,737✔
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;
264,474,670✔
2886
      pResultInfo->payloadLen = htonl(pRsp->compLen);
264,474,893✔
2887
      if (pRsp->compLen != pRsp->payloadLen) {
264,474,909✔
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;
291,377,830✔
2896

2897
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
291,378,020✔
2898
  return code;
291,374,581✔
2899
}
2900

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3044
  return TSDB_CODE_SUCCESS;
1,352✔
3045
}
3046

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

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

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

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

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

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

3075
      break;
1,352✔
3076
    }
3077

3078
    if ('`' == *(tbList + i)) {
5,408✔
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,408✔
3092
      if (vPos[vIdx] < 0) {
×
3093
        vPos[vIdx] = i;
×
3094
      }
3095
      continue;
×
3096
    }
3097

3098
    if ('.' == *(tbList + i)) {
5,408✔
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,408✔
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,408✔
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,408✔
3139
        ('0' <= *(tbList + i) && '9' >= *(tbList + i)) || ('_' == *(tbList + i))) {
676✔
3140
      if (vLen[vIdx] > 0) {
5,408✔
3141
        goto _return;
×
3142
      }
3143
      if (vPos[vIdx] < 0) {
5,408✔
3144
        vPos[vIdx] = i;
1,352✔
3145
      }
3146
      continue;
5,408✔
3147
    }
3148

3149
    goto _return;
×
3150
  }
3151

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

3166
  taosHashCleanup(pHash);
1,352✔
3167

3168
  return TSDB_CODE_SUCCESS;
1,352✔
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,352✔
3187
  SSyncQueryParam* pParam = param;
1,352✔
3188
  pParam->pRequest->code = code;
1,352✔
3189

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

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

3199
  if (pParam->pRequest) {
1,147,792,333✔
3200
    pParam->pRequest->code = code;
1,147,761,207✔
3201
    clientOperateReport(pParam->pRequest);
1,147,777,858✔
3202
  }
3203

3204
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
1,147,704,505✔
3205
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
3206
  }
3207
}
1,147,858,467✔
3208

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

3217
    return;
196✔
3218
  }
3219

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

3228
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, sql:%s", connId, sql);
1,147,357,680✔
3229

3230
  SRequestObj* pRequest = NULL;
1,147,357,680✔
3231
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, 0);
1,147,379,153✔
3232
  if (code != TSDB_CODE_SUCCESS) {
1,147,342,167✔
3233
    terrno = code;
3,355✔
3234
    fp(param, NULL, terrno);
3,355✔
3235
    return;
3,355✔
3236
  }
3237

3238
  code = connCheckAndUpateMetric(connId);
1,147,338,812✔
3239
  if (code != TSDB_CODE_SUCCESS) {
1,147,339,395✔
3240
    terrno = code;
584✔
3241
    fp(param, NULL, terrno);
584✔
3242
    return;
584✔
3243
  }
3244

3245
  pRequest->source = source;
1,147,338,811✔
3246
  pRequest->body.queryFp = fp;
1,147,331,151✔
3247
  doAsyncQuery(pRequest, false);
1,147,347,920✔
3248
}
3249

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

3258
    return;
9✔
3259
  }
3260

3261
  size_t sqlLen = strlen(sql);
301✔
3262
  if (sqlLen > (size_t)tsMaxSQLLength) {
301✔
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);
301✔
3270

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

3279
  code = connCheckAndUpateMetric(connId);
292✔
3280

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

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

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

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

3298
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
1,147,224,766✔
3299
  if (NULL == param) {
1,147,237,351✔
3300
    return NULL;
×
3301
  }
3302

3303
  int32_t code = tsem_init(&param->sem, 0, 0);
1,147,237,351✔
3304
  if (TSDB_CODE_SUCCESS != code) {
1,147,214,709✔
3305
    taosMemoryFree(param);
×
3306
    return NULL;
×
3307
  }
3308

3309
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
1,147,214,709✔
3310
  code = tsem_wait(&param->sem);
1,147,141,952✔
3311
  if (TSDB_CODE_SUCCESS != code) {
1,147,237,377✔
3312
    taosMemoryFree(param);
×
3313
    return NULL;
×
3314
  }
3315
  code = tsem_destroy(&param->sem);
1,147,237,377✔
3316
  if (TSDB_CODE_SUCCESS != code) {
1,147,231,596✔
3317
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3318
  }
3319

3320
  SRequestObj* pRequest = NULL;
1,147,244,184✔
3321
  if (param->pRequest != NULL) {
1,147,244,184✔
3322
    param->pRequest->syncQuery = true;
1,147,231,429✔
3323
    pRequest = param->pRequest;
1,147,239,322✔
3324
    param->pRequest->inCallback = false;
1,147,238,636✔
3325
  }
3326
  taosMemoryFree(param);
1,147,247,119✔
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,147,237,648✔
3332
}
3333

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

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

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

3369
static void fetchCallback(void* pResult, void* param, int32_t code) {
287,379,576✔
3370
  SRequestObj* pRequest = (SRequestObj*)param;
287,379,576✔
3371

3372
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
287,379,576✔
3373

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

3377
  pResultInfo->pData = pResult;
287,379,051✔
3378
  pResultInfo->numOfRows = 0;
287,379,051✔
3379

3380
  if (code != TSDB_CODE_SUCCESS) {
287,378,736✔
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) {
287,378,736✔
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,
292,807,640✔
3394
                                         pResultInfo->convertUcs4, pRequest->stmtBindVersion > 0);
287,378,899✔
3395
  if (pRequest->code != TSDB_CODE_SUCCESS) {
287,376,534✔
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(
287,376,390✔
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;
287,377,937✔
3405
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
287,379,510✔
3406
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
287,379,394✔
3407
  }
3408

3409
  pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows);
287,379,412✔
3410
}
3411

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

3416
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
324,917,951✔
3417

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

3424
    return;
2,299,272✔
3425
  }
3426

3427
  // all data has returned to App already, no need to try again
3428
  if (pResultInfo->completed) {
324,917,951✔
3429
    CLIENT_UPDATE_REQUEST_PHASE_IF_CHANGED(pRequest, QUERY_PHASE_FETCH_RETURNED);
75,076,630✔
3430
    // it is a local executed query, no need to do async fetch
3431
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
37,538,315✔
3432
      if (pResultInfo->localResultFetched) {
1,744,642✔
3433
        pResultInfo->numOfRows = 0;
872,321✔
3434
        pResultInfo->current = 0;
872,321✔
3435
      } else {
3436
        pResultInfo->localResultFetched = true;
872,321✔
3437
      }
3438
    } else {
3439
      pResultInfo->numOfRows = 0;
35,793,673✔
3440
    }
3441

3442
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
37,538,315✔
3443
    return;
37,538,315✔
3444
  }
3445

3446
  SSchedulerReq req = {
287,379,155✔
3447
      .syncReq = false,
3448
      .fetchFp = fetchCallback,
3449
      .cbParam = pRequest,
3450
  };
3451

3452
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
287,379,155✔
3453
  if (TSDB_CODE_SUCCESS != code) {
287,379,422✔
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,147,764,952✔
3460
  pRequest->inCallback = true;
1,147,764,952✔
3461

3462
  int64_t this = pRequest->self;
1,147,803,222✔
3463
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
1,147,722,871✔
3464
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
29,930✔
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,147,764,665✔
3476
           pRequest);
3477

3478
  if (pRequest->body.queryFp != NULL) {
1,147,765,286✔
3479
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
1,147,776,893✔
3480
  }
3481

3482
  SRequestObj* pReq = acquireRequest(this);
1,147,855,244✔
3483
  if (pReq != NULL) {
1,147,891,047✔
3484
    pReq->inCallback = false;
1,145,458,341✔
3485
    (void)releaseRequest(this);
1,145,458,929✔
3486
  }
3487
}
1,147,851,177✔
3488

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

3498
void updateConnAccessInfo(SConnAccessInfo* pInfo) {
1,248,290,408✔
3499
  if (pInfo == NULL) {
1,248,290,408✔
3500
    return;
×
3501
  }
3502
  int64_t ts = taosGetTimestampMs();
1,248,362,396✔
3503
  if (pInfo->startTime == 0) {
1,248,362,396✔
3504
    pInfo->startTime = ts;
101,010,426✔
3505
  }
3506
  pInfo->lastAccessTime = ts;
1,248,354,886✔
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